[Tutor] printing items form list
Antonio Zagheni
zagheni at yahoo.com
Fri Mar 3 13:20:32 EST 2017
Hello Rafael,
I believe you are a beginner...
That is another way to do this...-----------------------------------------------------------------------------------------------------
suitcase = ["book, ", "towel, ", "shirt, ", "pants"]
st = ''
for i in suitcase:
st = st + i
print ("You have a %s in your luggage.") %st
------------------------------------------------------------------------------------------------------------
Best regards...
Antonio Zagheni.
From: "tutor-request at python.org" <tutor-request at python.org>
To: tutor at python.org
Sent: Friday, March 3, 2017 2:00 PM
Subject: Tutor Digest, Vol 157, Issue 8
Send Tutor mailing list submissions to
tutor at python.org
To subscribe or unsubscribe via the World Wide Web, visit
https://mail.python.org/mailman/listinfo/tutor
or, via email, send a message with subject or body 'help' to
tutor-request at python.org
You can reach the person managing the list at
tutor-owner at python.org
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Tutor digest..."
Today's Topics:
1. printing items form list (Rafael Knuth)
2. Re: printing items form list (Peter Otten)
----------------------------------------------------------------------
Message: 1
Date: Fri, 3 Mar 2017 13:12:23 +0100
From: Rafael Knuth <rafael.knuth at gmail.com>
To: "Tutor at python.org" <Tutor at python.org>
Subject: [Tutor] printing items form list
Message-ID:
<CAM-E2X6F17Pop34ieWGU-0Cj+CCxK3b-VCDWz1atKTPU1u9wiw at mail.gmail.com>
Content-Type: text/plain; charset=UTF-8
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?
== RESTART: C:/Users/Rafael/Documents/01 - BIZ/Python/Python Code/PPC_7.py ==
You have a ['book', 'towel', 'shirt', 'pants'] in your luggage.
------------------------------
Message: 2
Date: Fri, 03 Mar 2017 13:30:23 +0100
From: Peter Otten <__peter__ at web.de>
To: tutor at python.org
Subject: Re: [Tutor] printing items form list
Message-ID: <o9bnkp$cam$1 at blaine.gmane.org>
Content-Type: text/plain; charset="ISO-8859-1"
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>
------------------------------
Subject: Digest Footer
_______________________________________________
Tutor maillist - Tutor at python.org
https://mail.python.org/mailman/listinfo/tutor
------------------------------
End of Tutor Digest, Vol 157, Issue 8
*************************************
More information about the Tutor
mailing list