Python Print Variable

Python is one of the most versatile programming languages, and we can use the print statement in several ways to print a variable in Python.

In this article, let us look at different approaches available in Python to print a variable with examples.

How to use the print() function in Python?

In Python, we can use the print statement to print any variable, strings, and other data types provided in Python, or we can use it for debugging the code.

The syntax of the print statement differs from Python 2 to Python 3. 

In Python 2, the print statement is pretty straightforward. You need to use the print keyword and input the message, as shown in the example.

Example – Print statement in Python 2 

# print statement in Python 2
print "Welcome to Python Tutorials"

Output

Welcome to Python Tutorials

In Python 3, you need to use the print statement as a method, which means the message you need to print should be wrapped in the parenthesis, as shown in the example.

Example – Print statement in Python 3

# print statement in Python 3
print("Welcome to Python Tutorials")

Output

Welcome to Python Tutorials

Note: In Python 2, we can run the print statement with and without parenthesis. However, in Python 3, we can run the print statement as a method; otherwise, you will get an error.

How to Print variable in Python?

There are 5 different approaches to print variables in Python. Let us explore each of the methods in depth with examples.

  • Using comma , character 
  • Using string Formatting with the help of %
  • Using string Formatting with the help of {}
  • Using the + operator
  • Using the f-string 

Method 1: Using comma , character to separate the variables in a print statement

The simplest and naive way to print the variable in Python is to separate the variable using the comma character in the print statement.

The following example will print the variable and string in the same line in Python code.

Example

# printing string with print statement using comma ","
first_name = "Bing"
last_name = "Chandler"
print("The full name of the Employee is ", last_name, first_name)

Output

The full name of the Employee is  Chandler Bing

Method 2: Using the String Formatting with the help of % character

The string formatting is popular in other languages, and Python also gives multiple ways to string formatting and print them.

String Formatting gives the user more options to customize, substitute, and place the variable in the right place to make the output of the print message more meaningful.

For example, we can use %s to substitute the string variable and %d to substitute the numerical value.

# print statement using string formatting 
first_name = "Bing"
last_name = "Chandler"
age =25
print("The full name of the Employee is %s %s and the age is %d " %(last_name, first_name,age))

Output

The full name of the Employee is Chandler Bing and  age is 25 

Method 3: Using the string formatting with positional arguments {}

We can use Python format specifiers as replacement fields within the string format. Starting from Python 3.1, the positional argument specifiers can be omitted.

For example, until Python 3.1, we had to pass the positional specifiers as arguments to the print function as shown below.

Example

# string formatting without positional specifiers
first_name = "Bing"
last_name = "Chandler"
age =25
print("The full name of the Employee is {0} {1} and the age is {2} ".format(last_name, first_name,age))
print("The full name of the Employee is {1} {0} and the age is {2} ".format(last_name, first_name,age))

Output

The full name of the Employee is Chandler Bing and the age is 25 
The full name of the Employee is Bing Chandler and the age is 25 

From Python 3.1 onwards, the positional specifiers can be omitted, and the same can be written as shown in the below example.

Example

# string formatting without positional specifiers
first_name = "Bing"
last_name = "Chandler"
age =25
print("The full name of the Employee is {} {} and the age is {} ".format(last_name, first_name,age))

Output

The full name of the Employee is Chandler Bing and the age is 25 

Note: We can use the positional specifiers in Python 3.1 and above as it has its own advantage. In fact, it works in all the Python versions.

Method 4: Using the f-string in the print statement

Another simplest way is to use f-string denoting with the letter f, which indicates Python, to format the string.

The f-string is another method of achieving string formatting, and it is comparatively faster when compared to other string formatting methods.

Example

# print statement using f-string
first_name = "Bing"
last_name = "Chandler"
age =25
print(f"The full name of the Employee is {last_name} {first_name} and the age is {age} ")

Output

The full name of the Employee is Chandler Bing and the age is 25 

Method 5: Using the + operator to join variables

The + operator is a mathematical operator, but you can use it in the print statement to join the variables. This basically is a string concatenation that concatenates the variables and string in Python.

Example

# print statement using the + character
first_name = "Bing "
last_name = "Chandler "
age =25
print("The full name of the Employee is " +last_name + first_name +" and the age is "+str(age))

Output

The full name of the Employee is Chandler Bing  and the age is 25
Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like
Python String Isdecimal()

Python String isdecimal()

Table of Contents Hide isdecimal() Syntaxisdecimal() Parametersisdecimal() Return ValueExample 1: Working of isdecimal()Example 2: String Containing digits and Numeric Characters The Python String isdecimal() method is a built-in function that…
View Post
Check Numpy Version

How to Check NumPy Version

Table of Contents Hide Check NumPy version using __version__ attributeGet the Detailed Version of NumPy Check NumPy version using pip3Check NumPy version on Anaconda Distribution.Print NumPy version in Command LineConclusion There…
View Post
Python Ternary Operator

Python Ternary Operator

Python Ternary operators also called conditional expressions, are operators that evaluate something based on a binary condition. Ternary operators provide a shorthand way to write conditional statements, which makes the…
View Post
Python Split List Into Chunks

Python Split list into chunks

Table of Contents Hide Python Split list into chunksMethod 1: Using a For-LoopMethod 2: Using the List Comprehension MethodMethod 3: Using the itertools MethodMethod 4: Using the NumPy MethodMethod 5:…
View Post