[Tutor] printing items form list
Peter Otten
__peter__ at web.de
Fri Mar 3 07:30:23 EST 2017
Rafael Knuth wrote:
> I want to print individual items from a list like this:
>
> You have a book, towel, shirt, pants in your luggage.
>
> This is my code:
>
> suitcase = ["book", "towel", "shirt", "pants"]
> print ("You have a %s in your luggage." % suitcase)
>
> Instead of printing out the items on the list, my code appends the
> list to the string. How do I need to modify my code?
Have a look at the str.join() method:
>>> suitcase = ["book", "towel", "shirt", "pants"]
>>> print("You have a %s in your luggage." % ", ".join(suitcase))
You have a book, towel, shirt, pants in your luggage.
See <https://docs.python.org/3.6/library/stdtypes.html#str.join>
More information about the Tutor
mailing list