How to turn a string into a list of integers?

obedrios at gmail.com obedrios at gmail.com
Wed Sep 3 16:30:28 CEST 2014


El miércoles, 3 de septiembre de 2014 05:27:29 UTC-7, c... at isbd.net  escribió:
> I know I can get a list of the characters in a string by simply doing:-
> 
> 
> 
>     listOfCharacters = list("This is a string")
> 
> 
> 
> ... but how do I get a list of integers?
> 
> 
> 
> -- 
> 
> Chris Green
> 
> ·

You Can Apply either, a map function or a list comprehension as follow:

Using Map:
>>> list(map(ord, listOfCharacters))
[84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 115, 116, 114, 105, 110, 103]

Using List Comprehension:

>>> [ord(n) for n in listOfCharacters]
[84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 115, 116, 114, 105, 110, 103]

Very Best Regards



More information about the Python-list mailing list