+ 1

How print work in this case?

words = ["hello", "world", "spam", "eggs"] #for words in words: print(words + "!") This snippet is not working words = ["hello", "world", "spam", "eggs"] for words in words: print(words + "!") This works how print different in both cases

23rd Sep 2019, 7:21 PM
B.G.PRADEEP KUMAR
B.G.PRADEEP KUMAR - avatar
2 Answers
+ 4
In: words = ["hello", "world", "spam", "eggs"] #for words in words: print(words + "!") You tried to concatenate a list ["hello", ... "eggs"] and a string "!", which raised an error. In: words = ["hello", "world", "spam", "eggs"] for words in words: print(words + "!") An indentation error would have been raised because a codeblock statement lacked required indent. Fixed. in: words = ["hello", "world", "spam", "eggs"] for words in words: print(words + "!") Program concatenated: "hello" + "!" "world" + "!" "spam" + "!" "eggs" + "!" And printed: hello! world! spam! eggs!
23rd Sep 2019, 8:02 PM
Seb TheS
Seb TheS - avatar
+ 3
Because the first for is an comment?
23rd Sep 2019, 7:49 PM
🍇 Alex Tușinean 💜
🍇 Alex Tușinean 💜 - avatar