Formating a string from a list


#1

Hi there,

I’m doing the The Boredless Tourist exercise. I have an issue at the very end. I won’t post the exercice here since it’s not really related to it.

I am supposed to print a string output basically saying (Hello (var_name), welcome to (var_destination), enjoy (list[activitie1, activitie2,…])

it’s pretty easy to format the name and destination, since it’s a variable containing only 1 word. But for the list, I have a hard time to format it to print it the proper way ( in Paris : Eiffel Tower, Louvres, Arc de Triomphe.) because I don’t know how to properly add space and comma.

I have done this :

for x in traveler_attractions:
att_string += " "+x
interests_string = “Hi %s, we think you’ll like these places around %s:%s.” %(traveler[0], traveler[1], att_string)

but I don’t know how to add a coma between each word of the list, without having one after the last word of the list resulting in an awful “,.”

If someone can help, I’ll be glad !


#2

Hi @xkimox,

You have several options. One of them is to use the str.join() method instead of a loop, as follows …

  interests_string = ("Hi " + traveler[0] +
                      ", we think you'll like these places around " +
                      traveler_destination + ": " +
                      ", ".join(traveler_attractions)
                     )