How to parse a string completely into a list
tino at wildenhain.de
Thu Sep 25 09:51:07 CEST 2008
john.ford at colorado.edu wrote:
> On Sep 24, 10:12 pm, Matt Nordhoff <mnordh... at mattnordhoff.com> wrote:
>> john.f... at colorado.edu wrote:
>>> On Sep 24, 9:44 pm, "Chris Rebert" <c... at rebertia.com> wrote:
>>>> Could you please define exactly what you mean by "elements" of a string?
>>>> If you mean characters, then just use list():>>> list(" \n \t abc")
>>>> [' ', ' ', '\n', ' ', '\t', ' ', 'a', 'b', 'c']
>>> Worked like a charm.
>> Why do you need to convert it to a list? Strings are sequences, so you
>> can do things like slice them or iterate through them by character:
>>>>> for character in "foo":
>> ... print character
> The string draws a map that I then want to be able to traverse
> through. If I can count through the individual characters of a list I
> can create an x-y coordinate plane for navigation.
You can 'count' (whatever that means) equally in strings as you do in
lists. As said above, they behave exactly the same. Just strings
are imutable - e.g. you can't change individual parts of them.
-------------- next part --------------
A non-text attachment was scrubbed...
Size: 3241 bytes
Desc: S/MIME Cryptographic Signature
More information about the Python-list