The entire purpose of variables is to store a value in them, perform some operations on those variables and output them to the user. So in this article, we will look at how to output variables to the user i.e Printing variables in Python.
The process of printing single and multiple variables is different in Python 2 and 3. While we recommend you to use Python 3 since Python 2 will be outdated in a while, we will still look at how to print variables in both these version to get a better understanding of Python.
Question Haunting: Why will Python 2 be outdated?
Check it here: Python 2 Vs 3
In Python 2, the variables are printed using a print statement. Whereas in Python 3, print statement is changed to print() function and hence this print() is used.
To print a single variable in Python 2.x, you can write down the print statement in the below mentioned two ways and both of these will get you the correct output.
#printing single variable in Python num1 = 10 print num1 num2 = 9.8 print (num2)#printing single variable in Python
Output: 10 9.8
For printing multiple variables in Python 2, the variable names have to be comma-separated in the print statement as shown below.
#printing multiple integer variables in Python num1, num2 = 10, 20 print num1, num2 #printing multiple string variables in Python print "FACE","Prep","Python" print "1", "2","3", "G
Output: 10 20 FACE Prep Python 1 2 3 Go
In Python 2, if multiple variables separated by commas are enclosed within round brackets in print statement, then the compiler will consider those two variables as a tuple (Tuple is nothing but a list which can hold values of different types like float, integer, string etc) and print them as comma-separated values within round brackets.
#printing multiple integer variables in Python num1, num2 = 10, 20 print (num1, num2) #printing multiple string variables in Python print ("FACE","Prep","Python") print ("1", "2","3", "Go")
Output: (10, 20) ('FACE', 'Prep', 'Python') ('1', '2', '3', 'Go')
Since Python 3 uses print () function, the variable has to be enclosed by round brackets in the print statement mandatorily. If not, the compiler will throw an error.
#printing single integer variable in Python 3 num1 = 10 print (num1) print (num1 + 10) print ("7") print ("FACE Prep")
Output:10 20 7 FACE Prep
#Printing single variable in Python 3 without () num2 = 8 print num2
Output: File "main.py", line 6 print num2 ^ SyntaxError: Missing parentheses in call to 'print'
To print multiple variables in Python 3, variable names have to enclosed within round brackets and mentioned as comma-separated. This is again mandatory.
#printing single integer variable in Python 3 num1, num2 = 10, 20 print (num1, num2) print ("7", "8", "Nill") print ("FACE Prep", "is the best")
Output: 10 20 7 8 Nill FACE Prep is the best
In Python 3, if we enclose the above variables within one more round brackets in the print function, then those two variables are considered as tuples and printed as shown below.
#printing single integer variable in Python 3 num1, num2 = 10, 20 print ((num1, num2)) print (("7", "8", "Nill")) print (("FACE Prep", "is the best"))
Output:(10, 20)('FACE', 'Prep', 'Python')('1', '2', '3', 'Go')
1) How to print a variable along with some text? or How to print variable along with a string in Python?
(These will be in Python 3 only)
Let us say you want to print this statement: I Learnt Python on FACE Prep in 20 days. Now there are two simple methods to do this:
a = 20b = FACE Prep print (I Learnt Python on, b, in,a, days )
a = 20b = FACE Prep print(I Learnt Python on %s in %d days %(b,a))
Output for Method 1 & Method 2:
I Learnt Python on FACE Prep in 20 days Also, there are other methods to output variables along with a string or text and you can check them out here.