Python Python Basics (2015) Logic in Python Loop

Unable to concatenate List containing Strings with String.

I am working on the exercise, in which I have to create a For loop that will Print each String within the List called hellos and append the String "World" via concatenation.

I'm able to get the Strings in hellos to print without issue but run into trouble when I add + "World".

When I run the code, the String "Hello" will output and then I get the following error:

Traceback (most recent call last):
File "<stdin>", line 2, in <module>
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

If I'm reading this correctly, I need to change the list from NoneType to String, but I'm not sure how.
hellos = [
    "dobrý den",
    "hyvää päivää",
for word in hellos:
    print(word) + "World"

1 Answer

28,492 Points

The issue is actually that you are trying to append "World" to what the print function returns, rather than the word string due to the + being placed outside the parenthesis of the print call.

If you place it inside the parenthesis then you won't get that error, but there is another issue. Which is the fact that when you concatenate a string the two strings are glued together without any spaces. So "Hello" + "World" for example would produce "HelloWorld", in order to get correct spacing you have to manually add a space.

Like this for example:

for word in hellos:
    print(word + " World")

Ah, it's so clear now! Thank you!