Print Single and Multiple variable in Python

Consider below two Python code snippets in Python 2.x.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Code 1
print 1
# Output: 1

chevron_right


filter_none

edit
close

play_arrow

link
brightness_4
code

# Code 2
print(1)
# Output: 1

chevron_right


There is no difference between code 1 and code 2 in case of single variable in Python 2.X, but in case of multiple variables, variable with brackets -() is treated as “tuple”.

For multiple variable:

  • “print variable” prints the variables without any brackets ‘()’ and splitted by a space
  • “print(variable)” prints the variables with brackets ‘()’ and splitted by a coma ‘,’ so it’s treated as a tuple.

Examples-

filter_none

edit
close

play_arrow

link
brightness_4
code

# Code 3
print 1, 2
# Output: 1 2

chevron_right


filter_none

edit
close

play_arrow

link
brightness_4
code

# Code 4
print (1, 2)
# Output: (1, 2)

chevron_right



Note:

In Python 3.0, the print statement is changed to print() function. Below are equivalent codes in Python 3.0.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Equivalent codes in Python 3.0
# (Produces same output)
  
# Code 1:
print(1)
# Output: 1
  
# Code 2 :
print((1))
# Output: 1
  
# Code 3:
print(1, 2)
# Output: 1 2
  
# Code 4:
print((1, 2))
# Output: (1, 2)

chevron_right


This article is contributed by Arpit Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above



My Personal Notes arrow_drop_up


Article Tags :

4


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.