In this tutorial, I will show you how to print string and integer on the same line.
- The string is basically a sequence of characters enclosed in single or double quotes like (‘ ‘ , ” “).
- Integers are positive or negative whole numbers that are represented by (int).
So, in Python printing String and Integer is so much easy. We can do it by following some easy steps.
- At first, I initialize a string which is Haswati enclosed in double quotes.
- Basically the string is denoted by the variable whose name is string.
- After that, I declare the integer 21.
- Then print both variables together. (The process of printing is basically concatenate both strings and add some lines like Hello!! myself and my age is. Also, store the integer variable in the string function.)
string= "Haswati" integer= 21 print("Hello guys!! myself " + string + " My age is " + str(integer))
So, the result is very simple.
Output:
Hello guys!! myself Haswati My age is 21
In this program, I use the concatenate method. Also, we can do it in various way like- f – string, format ( ), string. join
*NOTE:
Example of f – string:
h=f"My name is Haswati" print(h)
OUTPUT:
My name is Haswati
Example of format( ):
h1="My name is {hname}, age is {age}".format(hname="Haswati", age=21) print(h1)
OUTPUT:
My name is Haswati, age is 21
Examples of string. join:
x = {"Haswati":"name", "Naskar":"surname"} y ="and" z=y.join(x) print(z)
Output:
HaswatiandNaskar
These are the various ways to do this
For better learning and coding you can check the link given below-
Have a Happy and a Great Coding!