Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between comma and plus in python print statements? [closed]

I wish to understand the output of print((x,y)*5). I wish to know what actually is happening behind the scenes. (To me, it seems as if it turned into a list. Am I right?)

x = 'Thank'
y = 'You'

print(x+y)
print(x,y)

print((x+y)*5) 
print((x,y)*5) #This one..

I am a beginner in Python and I'm asking a question for the first time ever. Please forgive me if this question seems naive. I tried Googling but it didn't help.

like image 852
Vijay Sharma Avatar asked Feb 13 '19 06:02

Vijay Sharma


People also ask

How do you print with commas in Python 3?

In Python 3, we get a print function, which we can also use in Python 2. The print statement with commas separating items, uses a space to separate them. A trailing comma will cause another space to be appended. No trailing comma will append a newline character to be appended to your printed item.

What happens if you put a comma in a print statement?

Print Statements with Commas (Python 2) The print statement with commas separating items, uses a space to separate them. A trailing comma will cause another space to be appended. No trailing comma will append a newline character to be appended to your printed item.

What are the different styles of print statements in Python programming?

Different Styles Of Print Statements In Python Programming. 1 1)Numbered Indexes PROGRAM OUTPUT The two numbers are 10 and 20. 2 2)Named Indexes PROGRAM OUTPUT. 3 3)EMPTY PLACEHOLDER PROGRAM OUTPUT.

What is the difference between Python 2 and Python 3?

In Python 2, we had print statements. In Python 3, we get a print function, which we can also use in Python 2. The print statement with commas separating items, uses a space to separate them. A trailing comma will cause another space to be appended. No trailing comma will append a newline character to be appended to your printed item.


Video Answer


1 Answers

x = 'Thank'
y = 'You'


# concatenation of string x and y which is treated as a single element and then 
# printed (Here, only a single argument is passed to the print function).
print(x+y)  
# Output: ThankYou  (Simple String concatenation)


# Here, two different arguments are passed to the print function.
print(x,y)  
# Output python 2: ('Thank', 'You')  (x and y are treated as tuple
# Output python 3: Thank You  (x and y are comma seperated and hence, 
# considered two different elements - the default 'sep=" "' is applied.)


# The concatenated result of (x + y) is printed 5 times.
print((x+y)*5) 
# Output: ThankYouThankYouThankYouThankYouThankYou  

# x and y are made elements of a tuple and the tuple is printed 5 times.
print((x,y)*5) 
# Output: ('Thank', 'You', 'Thank', 'You', 'Thank', 'You', 'Thank', 'You', 'Thank', 'You')  
like image 130
taurus05 Avatar answered Oct 10 '22 04:10

taurus05