[Tutor] Using string.strip()
Koen
koen@behindthesofa.dhs.org
Wed, 08 Aug 2001 12:14:25 +0200
Sheila King wrote:
>OK, I'm having another one of my dumb moments...
>
>
>Now, that will leave leading and trailing '\n' characters on the items
>in the responses list, so I try this:
>
>for item in responses:
> item = item.strip()
>
>But when I run it and print out the results, I still have leading and
>trailing '\n' characters on the individual list elements, as though the
>strip() function had no effect.
>
This is not a problem with the strip or split command.
the problem is in the for loop. see the following examples:
for item in responses:
item = item.strip()
for item in range(len(responses)):
responses[item] = responses[item].strip()
in the first example, second line, the variable /item /is in no way
related with the list /responses/.
in the second example, you change the elements /in the list/ itself.
You should ALWAYS use this second method when altering contents of lists!!
Cheers, Koen