How to turn a string into a list of integers?

cl at isbd.net cl at isbd.net
Wed Sep 3 10:48:01 EDT 2014


Peter Otten <__peter__ at web.de> wrote:
> cl at isbd.net wrote:
> 
> > 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?
> > 
> 
> >>> [ord(c) for c in "This is a string"]
> [84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 115, 116, 114, 105, 110, 103]
> 
> There are other ways, but you have to describe the use case and your Python 
> version for us to recommend the most appropriate.
> 
That looks OK to me.  It's just for outputting a string to the block
write command in python-smbus which expects an integer array.

Thanks.

-- 
Chris Green
·



More information about the Python-list mailing list