how to print a variable and a string on the same line in python

When we have multiple strings or variables and we want to combine their data into a single variable, list, array, or any other data structure, it is called data concatenation. There are different methods by which you can combine the string and integer variables and print them on the same line. The topic of this article is How to print a variable and string on the same line in python. We will learn about the concatenation of strings with integers. If you want to learn more about Python Programming, visit Python Programming Tutorials.

What are the common methods to print a variable and string on the same line in python?

To print a variable and string on the same line in python, following methods are used:

  1. Use +str() to Print a String and variable on the same line
  2. Use .format(str()) to Print a String and variable on the same line
  3. Use % for Printing a String and a variable on the same line
  4. Print Strings with f'{}

use + str() to Print a String and variable on the same line

The + operator is used to concatenate two strings. To concatenate an integer with a string, you need to convert an integer into a string using str() function. Suppose you are asked to input your roll number. You want to display that ‘My Roll Number is 23’. Now, in this case, 23 is an integer and is taken from the user as an input. The example below demonstrates the concatenation procedure.

# Using + str()

Roll_Number = int(input("Enter your roll number: "))
print("My Roll Number is " + str(Roll_Number))
Enter your roll number: 23
My Roll Number is 23

Printing String and variable on the same line using .format(str())

Another way is to use format() method. A placeholder is defined in a string using curly brackets. The format() method takes an input and insert that value inside the string’s placeholder. Consider the same example discussed in the previous section. But, now concatenate the string and input integer variable using format() method.

# Using .format(str())

Roll_Number = int(input("Enter your roll number: "))
print("My Roll Number is {0}".format(str(Roll_Number)))
Enter your roll number: 23
My Roll Number is 23

Not only at the end but the format method can also insert the data at start, middle or anywhere in the string. Consider another example in which you have multiple inputs.

# Using .format(str())

name = input("Enter your name: ")
Roll_Number = int(input("Enter your roll number: "))
print("My name is {0} and my Roll Number is {1}".format(name,str(Roll_Number)))
Enter your name: Ali
Enter your roll number: 12
My name is Ali and my Roll Number is 12

use %

The ‘%’ operator is also used to concatenate an integer and a string. The variable with “%” sign at the start is replaced with the integer as shown below.

# Using %

Roll_Number = int(input("Enter your roll number: "))
print("My Roll Number is %s."%Roll_Number)
Enter your roll number: 23
My Roll Number is 23.

Print different Strings with f'{}

The last but not the least is to use f'{} method. The f or F is used in front of strings. It tells the python compiler to look at the variable inside the curly brackets and substitute the value of that variables if it exists.

# Using f'{} method

name = 'Faizan'
print(f'Hey my name is {name}.')
Hey my name is Faizan.

In this article, we have learned different ways of concatenating strings with integers. You have also seen how to insert the integers in different locations of the string. If you want us to cover any other topic related to this one, please let us know. For feedback, contact us.

Leave a Comment

Your email address will not be published.