Print a variable & string on the same line in Python

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.

While using print() function in Python, the comma (,) is used to combine two or more variables without any separator.

Syntax:

Advertisements
print(input1,input2,.............)

where input is the variable that can be string or integers.

Example 1:

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.

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 ‘-‘.

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.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top