Skip to content
Advertisement

What is point of using the concatenation(+) in Python when commas(,) do the job?

print("This is a string" + 123)

Concatenating throws error, but using a comma instead does the job.

Advertisement

Answer

As you already been told, your code raises an error because you can only concatenate two strings. In your case one of the arguments of the concatenation is an integer.

print("This is a string" + str (123))

But your question is more something “plus vs. comma”. Why one should ever use + when , works?

Well, that is true for print arguments, but actually there are other scenario in which you may need a concatenation. For example in an assignment

A = "This is a string" + str (123)

Using comma, in this case, would lead to a different (and probably unexpected) result. It would generate a tuple and not a concatenation.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement