[Tutor] Using string.strip()
Sheila King
sheila@thinkspot.net
Wed, 08 Aug 2001 00:14:19 -0700
On Wed, 8 Aug 2001 09:15:28 +0200, Hans Nowak <hnowak@cuci.nl> wrote
about RE: [Tutor] Using string.strip():
:Note that this for loop "as is" doesn't do anything to the responses list...
:it just strips an item, then discards it. Maybe this is the reason why it
:doesn't have the desired effect?
Thanks to all who responded and pointed this out.
As I said, I was having a dumb moment. I certainly should have known
better.
I especially liked the solution:
responses = [item.strip() for item in responses]
That is really the coolest. (I'm still not good at the list
comprehensions thing.)
On Wed, 08 Aug 2001 09:11:19 +0200, Roeland Rengelink
<r.b.rigilink@chello.nl> wrote about Re: [Tutor] Using string.strip():
:
:Maybe you can post a complete non-working example.
Certainly, I can:
Python 2.0 (#8, Oct 16 2000, 17:27:58) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
IDLE 0.6 -- press F1 for help
>>> import string
>>> messagebody = '''\
N
;;
no, cs student
;;
N
;;
no
;;
S
;;
case-sensitive
;;
Y
;;
yes
;;
N
;;
no
;;
'''
>>> responses = messagebody.split(';;')
>>> print responses
['N\012', '\012no, cs student\012', '\012N\012', '\012no\012',
'\012S\012', '\012case-sensitive\012', '\012Y\012', '\012yes\012',
'\012N\012', '\012no\012', '\012']
>>> for item in responses:
item = item.strip()
>>> print responses
['N\012', '\012no, cs student\012', '\012N\012', '\012no\012',
'\012S\012', '\012case-sensitive\012', '\012Y\012', '\012yes\012',
'\012N\012', '\012no\012', '\012']
>>>
Of course, as has been pointed out, I'm not doing anything to the list
itself, only to the items and not reassigning them back to the list.
Thanks to all for the swift responses. I will now go dent my head a
bit...
--
Sheila King
http://www.thinkspot.net/sheila/
http://www.k12groups.org/