Difference between using comma(,) and plus(+) in print statement

The following statements are outputting same result. However what is the difference between the below print statements?

print("welcome to ", a_string, "!")
print("welcome to " + a_string + "!")

In addition, when I run the statement as
print("welcome to ", a_string, “!”, 2)
It outputs “welcome to Dataquest ! 2”.
But print("welcome to " + a_string + “!” + 2) is throwing typeError as expected.

Please explain the difference.


@sumahmud: the comma in print() has a default behaviour of adding a space between 2 elements. The + symbol can be use for 2 purposes - addition and concatenation. In this case, it is used in the context of addition, but type string (i.e. "!") cannot be added to type int (i.e. 2), so the error is raised. You might need to check the following article as a refresher.

1 Like

Thanks a lot. Appreciate your explanation.

1 Like

No worries @sumahmud! Happy coding!