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 trialHeather 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?
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
21,074 PointsThere'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.
Carlos Federico Puebla Larregle
21,074 PointsIf 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
4,691 PointsThanks! I'll save this information for later. :)
Carlos Federico Puebla Larregle
21,074 PointsYou 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
Heather Gilchrist
4,691 PointsHeather Gilchrist
4,691 PointsOhhh! 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! :)
Heather Gilchrist
4,691 PointsHeather Gilchrist
4,691 PointsThanks so much! That worked.