+ 1

Python String question.

Question is: x = 9 y = "2" print (str(x)+y) Solution: 92 -A string must be surrounded by either single or double quotes? So shouldn't the answer just be "2"?

20th Mar 2019, 2:24 AM
tristach605
tristach605 - avatar
3 Respostas
+ 10
print(str(x) + y) str(x) turns x into a string, so it becomes '9'. Now, since both str(x) and y are strings, the '+' is interpreted as the string concatenation operation. '9' + '2' = '92', which is the output (when printing just a string the quotes are omitted). Hope that helps. 😊
20th Mar 2019, 2:41 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 3
You're welcome, tristach605 👍
20th Mar 2019, 3:36 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 2
Huge help! Thanks Saha!
20th Mar 2019, 3:06 AM
tristach605
tristach605 - avatar