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

'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

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.

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! :)

Thanks so much! That worked.

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

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

You are welcome Heather, here you have a link to the python docs for more information on those formats: https://docs.python.org/3/library/stdtypes.html?highlight=format#str.format