6

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.

5
  • plus concatenate and the other just display the variables value Commented Feb 13, 2019 at 6:20
  • try that with int or floats , You'll understand Commented Feb 13, 2019 at 6:21
  • x+y concatenate and print it 5 times, while x,y are two seperate values and are multiplied 5 times alternatively and displayed as list. so its like x first then y at second place upto 5 times Commented Feb 13, 2019 at 6:23
  • print("ab" + "ra" + "ca" + "da" + "bra") vs print("ab","ra","ca","da","bra") vs print("ab","ra","ca","da","bra", sep = "WUSH") - seperate entities are printed with sep between - concattenated strings are printed as one concatted string. And printing tuples (not lists) should be obvious. Commented Feb 13, 2019 at 6:24
  • Duplicate of stackoverflow.com/questions/21542694/… Commented Sep 11, 2020 at 16:47

2 Answers 2

8
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')  
Sign up to request clarification or add additional context in comments.

5 Comments

It would be worth explaining what is printed and why. You're halfway there.
Less then 10 months to skip using python 2.x: pythonclock.org - your answer is python 2 specific - the question is python 3, I edited the difference in. You can verify it using f.e. pyfiddle.io (switched to 3.x)
@PatrickArtner, Thanks for the edit!
Thank you so much for solving my doubts! This is crystal clear to me now.
Happy Learning!
0

plus act as concatenation operator if operands are string otherwise it acts as mathematical plus operator.Whereas comma in print function is used to separate the variables:In case of string , and + in print works as same but fundamentally they are different: + creates new object from operand whereas comma uses objects and did not create new :

print('Delhi'+'is capital')

print(2+6)

var1='Hello'
var2='!!'
print(var1,var2)
print(2,6)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.