Tutorialspoint.dev

Print Single and Multiple variable in Python

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

# Code 1
print 1
# Output: 1

# Code 2
print(1)
# Output: 1

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-

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

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


Note:

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

# 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)

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



This article is attributed to GeeksforGeeks.org

tags:

Python

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter