[Pythonmac-SIG] dumping characters into a list

Sheila sheila at thinkspot.net
Sat Jan 21 04:37:55 CET 2006


--On January 20, 2006 7:24:47 PM -0800 Sheila King <sheila at thinkspot.net> 
wrote:

> --On January 20, 2006 9:18:26 PM -0600 Silas Hundt <silashundt at gmail.com>
> wrote:
...
>> Receive input, cut that string up into individual characters (ALL
>> characters, including spaces), put them in order into a list, then
>> pull them out in order to convert them to a number.
...
>>>> s = "This is a string."
>>>> list(s)
> ['T', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ', 's', 't', 'r', 'i',
> 'n',  'g', '.']

I'm not sure what numbers exactly you want to convert the characters to, 
but the one that springs to my mind are the ordinals for each character. 
So, if that's what you want, you could further do this:

>>> chars = list(s)
>>> chars
['T', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ', 's', 't', 'r', 'i', 'n', 
'g', '.']
>>> ords = map(ord, chars)
>>> ords
[84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 115, 116, 114, 105, 110, 103, 
46]
>>>

-- 
Sheila King
sheila at thinkspot.net
http://www.thinkspot.net/sheila/



More information about the Pythonmac-SIG mailing list