Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialak37
2,344 Points"Hello World" For Loop Problem
Can anyone please tell me what is wrong with the loop I created? Thanks
hellos = [
"Hello",
"Tungjatjeta",
"Grüßgott",
"Вiтаю",
"dobrý den",
"hyvää päivää",
"你好",
"早上好"
]
for greeting in hellos:
print(greeting + "World.")
1 Answer
andren
28,558 PointsThe loop itself is fine, it's actually the print
statement that is a bit problematic. When you concatenate two string using the + operator they are combined without any added spaces. That means that "Hello" + "World"
for example would become "HelloWorld"
.
In order to get proper spacing you have to add a space manually. Like this for example:
for greeting in hellos:
print(greeting + " World")
Since there is now a space at the start of " World"
the spacing of the combined string will be correct.