uwon
uwon

Reputation: 47

What is the difference between printing two variables using "+" and "," in Python?

a = 2
b = 4
print(a, b)
print(str(a) +" " + str(b))

Is there a difference between the first print and the second print? If there is, which one should I use?

Upvotes: 0

Views: 96

Answers (1)

oo00oo00oo00
oo00oo00oo00

Reputation: 473

print(a, b) uses a and b as function parameters.

print(str(a) + str(b)) actually concatenates strings and then passes result to print() function.

That's the only difference.

But you can get the advantage from print(a, b) by using sep argument. This can be helpful when you're passing multiple arguments and you want all of them to be separated with certain text.

Ex:

a, b, c = 1, 2, 3
print(a, b, c, sep='--')   # prints 1--2--3

Upvotes: 2

Related Questions