Python Print Variable

  1. Use the print Statement to Print a Variable in Python
  2. Use a Comma , to Separate the Variables and Print Them
  3. Use the String Formatting With the Help of %
  4. Use the String Formatting With the Help of {}
  5. Use the + Operator to Separate Variables in the print Statement
  6. Use the F-String in the print Statement

Python is one of the most versatile programming languages, and the print statement can be used in many different ways. We can choose the way we want to use the statement according to our convenience.

This tutorial will discuss the different methods to print a variable in Python.

Use the print Statement to Print a Variable in Python

The print statement can be used in all Python versions, but its syntax varies, depending on Python’s version.

In Python 2, the syntax for the print statement is pretty simple. All we have to do is use the print keyword.

Example code:

#Python 2 code
print 'Hello to the World'

Output:

Hello to the World

In Python 3, to run the print statement without any errors, there needs to parenthesis followed by the print keyword.

Example code:

#Python 3 code
print('Hello to the World')

Output:

Hello to the World

Although Python 2 can run the print statement with parenthesis, Python 3 gives an error if the parenthesis is not used in the statement.

The print statement can be used to print variables, strings, and the other data types provided in Python.

We can print different data types in Python’s single statement by separating the data types or variables with the , symbol.

We will show different methods to print a variable in Python.

Use a Comma , to Separate the Variables and Print Them

The most basic method to print the variable in Python is to use a comma to separate the variables in a print statement.

The following code prints the variable and string in the same line in Python code.

var1 = 123
var2 = 'World'
print("Hello to the",var2,var1)

Output:

Hello to the World 123

Use the String Formatting With the Help of %

In addition to just printing the statements, string formatting can also be used. This gives the user more options to customize the string-like setting and adjust the padding, alignment, set precision, and even the width.

One of the two ways to implement string formatting is using the % sign. The % sign, followed by a letter, works as a placeholder for the variable.

For example, %d can be used as a placeholder when we need to substitute numerical values.

Example code:

var1 = 123
var2 = 'World'
print("Hello to the %s %d " %(var2,var1))

Output:

Hello to the World 123

Use the String Formatting With the Help of {}

When using string formatting, we can use {} to mark the place in the statement where the variable needs to be substituted.

The following code prints the variable and string in the same line in Python code by using string formatting:

var1= 123
var2= 'World'
print("Hello to the {} {}".format(var2,var1))

Output:

Hello to the World 123

The format() function is added to Python 2.6 and works in all other versions after that.

Use the + Operator to Separate Variables in the print Statement

The + operator can also be used to separate the variables while using the print statement.

Example code:

var1= 123
var2= 'World'
print('Hello to the {} {}' + var2 + str(var1))

Output:

Hello to the World 123

Use the F-String in the print Statement

The f-string is another method of achieving string formatting and has the edge over the other two methods as it is comparatively faster than the other two mechanisms.

Example code:

var1= 123
var2= 'World'
print(f'Hello to the {var2} {var1}')

Output:

Hello to the World 123

As shown above, all 5 methods provide the same output. And the method that one should use depends on his own convenience.

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Python Print

  • Print Blank Line in Python
  • Flush Print Output in Python