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 trial

Python Python Basics (2015) Logic in Python Loop

Heather Gilchrist
Heather Gilchrist
4,691 Points

'list' object has no attribute 'format'

I am trying to print out this list with "World." added at the end of it so "Hello World" will be printed, then "Tungjatjeta World" so on and so forth. However I keep getting the error "'list' object has no attribute 'format'" whenever i add the .format("World.") function at the end of hellos. Ive attempted this in a few places and i just cant seem to get this to work. Any idea what I am doing wrong?

loop.py
hellos = [
    "Hello",
    "Tungjatjeta",
    "Grüßgott",
    "Вiтаю",
    "dobrý den",
    "hyvää päivää",
    "你好",
    "早上好"
]

for word in hellos:
  print(hellos.format("World."))

3 Answers

Carlos Federico Puebla Larregle
Carlos Federico Puebla Larregle
21,074 Points

There's no need to use the "format()" function, and you have to print each "word" instead of the entire "hellos". Use the + operator to concatenate the word and the string " World". You can do it like this:

hellos = [
    "Hello",
    "Tungjatjeta",
    "Grüßgott",
    "Вiтаю",
    "dobrý den",
    "hyvää päivää",
    "你好",
    "早上好"
]

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

I hope that helps a little bit.

Heather Gilchrist
Heather Gilchrist
4,691 Points

Ohhh! Okay. I was confused because it was telling me 'list' object has no attribute 'format'. Assuming it required the Format function. I'll try this out. Thanks so much! :)

Carlos Federico Puebla Larregle
Carlos Federico Puebla Larregle
21,074 Points

If you really want to use format you can use it like this:

hellos = [
    "Hello",
    "Tungjatjeta",
    "Grüßgott",
    "Вiтаю",
    "dobrý den",
    "hyvää päivää",
    "你好",
    "早上好"
]

for word in hellos:
  print("{} World".format(word))

Using the placeholder to "place" the word

Heather Gilchrist
Heather Gilchrist
4,691 Points

Thanks! I'll save this information for later. :)