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
"!") cannot be added to type
2), so the error is raised. You might need to check the following article as a refresher.
Thanks a lot. Appreciate your explanation.
No worries @sumahmud! Happy coding!