Print Variable and String in Same Line in Python

💡 TL;DR

To Print Variable and String in Same Line in Python, use print() method and separate String and variable with comma.

Print Variable and String in Same Line in Python

We use the print() function in Python to display output. It was changed to a function in Python and existed as a statement in Python 2. Changing it to a function was very useful as it introduced parameters like end, sep, and more with this function.

We can display multiple values at once using the print() function in Python.

This tutorial will demonstrate how to print variable and string in same line in Python.

Using a comma

We can use a comma to print variable and string in same line in Python. We will separate the two values using the comma within the function.

For example,

Output:

Java 2 Blog

Note the space between the string and variable in the output. This is due to the sep parameter within the print() function which sets the separator character between two values and is set to a space character by default.

Using the % formatting

Python allows us to format strings based on our requirements. We can provide the required format in three different ways.

In the first method, we will use the % specifier. We can use this as a placeholder for different values. For example, the %s can act as a placeholder for a string variable, %d can act as a placeholder for an integer, and more.

Let us see how we can use this to print variable and string in same line in Python.

Output:

Java2Blog

In the above example, we use the %d specifier as a placeholder for the variable value and add this in the string with the % formatting.

Using the format() function

The format() function provides another convenient method to format strings. With this function, we can use the curly braces as a placeholder for variable values within the string. The required variables can be specified within the function.

See the following example.

Output:

Java2Blog

Using the fstrings

Python 3 introduced a new and efficient way to format strings using the fstrings feature. It provides an easy and efficient way to format strings by using the prefix f before the string.

In this method also we will use the curly braces as placeholders within the string as we did in the previous method.

See the code below.

Output:

Java2Blog

Using the + operator and str() function

The + operator functions differently for different types. For integer and float values, we use this to perform arithmetic addition. However, it can be used to concatenate strings when we use this operator with string-type values.

The str() function in Python is used to convert a variable value to a string. We can use both these methods together to print variable and string in same line in Python.

We will convert the variable to a string and concatenate it with the string using the + operator.

For example,

Output:

Java2Blog

Conclusion

To conclude, we discussed several methods to print variable and string in same line in Python. We can directly separate the two using a comma as discussed in the first method. The following methods highlight the use of string formatting to achieve the same.

We discussed three formatting techniques. First, we used the % specifier where we specify missing variables separately and mention the % specifiers in their place in the string. The next two methods demonstrate the use of format() function and fstrings in Python, where we use the curly braces as a placeholder for missing variables.

In the last method, we demonstrated the use of the + operator and str() function to concatenate strings with the required variables in Python to print them in the same line.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *