In this Python tutorial, you will learn how to print a variable and string on the same line.
Table Of Contents
Let’s discuss some approaches to print a variable and string on the same line in Python.
Print a variable and string on the same line using the comma operator
While using print() function in Python, the comma (,) is used to combine two or more variables without any separator.
Syntax:
print(input1,input2,.............)
where input is the variable that can be string or integers.
Example 1:
Frequently Asked:
- Concatenate items in a List to a String
- Python: String to int
- How to Reverse a String in Python?
- Check if String is Lowercase in Python
In this example, we will print two variables using the, operator along with a string.
input_str1 = "Welcome to" input2 = 456 # Display the two variables along with string print(input_str1 , input2, "Are displayed")
Output:
Welcome to 456 Are displayed
We can see that two variables are displayed on the same line along with the string -“Are displayed”.
Example 2:
In this example, we will display three variables along with the string on the same line using the operator.
input_str1 = "Welcome to" input2 = 456 # Consider the below float variable input3 = 456.566 # Print the three variables on the same line print(input_str1 , input2, input3, "are on the same line")
Output:
Welcome to 456 456.566 are on the same line
We can see that three variables along with string are displayed on the same line.
Print a variable and string on the same line using %d and %s
The %s is used to specify the string variables. It is used to print string variables.
The %d specifies the integer variables. It is used to print int variables.
The %f specifies the float variables. It is used to print float variables.
Syntax:
"%s%d%f" % (input_str, input_integer, input_float)
where input_str is the input string, input_integer is the integer variable and input_float is the float variable.
Example 1:
In this example, we will print all variables on the same line.
# Consider the below string input_str1="Welcome to" # Consider the below integer input_2=45 # Consider the below float input_3=12.566 # Display all variables on same line print("Variables: %s%d%f" % (input_str1, input_2, input_3))
Output:
Variables: Welcome to4512.566000
All variables are printed on the same line.
Example 2:
In this example, we will print all variables on the same line by a separator ‘-‘.
# Consider the below string input_str1="Welcome to" # Consider the below integer input_2=45 # Consider the below float input_3=12.566 # Display all variables on same line with a separator print("Variables: %s - %d - %f" % (input_str1, input_2, input_3))
Output:
Variables: Welcome to - 45 - 12.566000
All variables are printed on the same line separated by ‘-‘.
Print a variable and string on the same line using f-strings
Here we will use f{} which will combine all variables that are placed inside {}.
Syntax:
f'string{input_1}{input_2}{input_3}.......string.....'
Example 1:
# Consider the below string input_str1="Welcome to" # Consider the below integer input_2=45 # Consider the below float input_3=12.566 # Display all variables print(f'Variables:{input_str1}{input_2}{input_3}')
Output:
Variables:Welcome to4512.566
Example 2:
Let’s declare three variables and display them using f-strings
# Consider the below string input_str1="Welcome to" # Consider the below integer input_2=45 # Consider the below float input_3=12.566 # Display all variables print(f'Variables:{input_str1}{input_2}{input_3} are displayed')
Output:
Variables:Welcome to4512.566 are displayed
Summary
We discussed how to print a variable and string on the same line in Python using comma, f-strings, and formatting operators like %s,%f, and %d. Happy Coding.