Convert string to ASCII value python

Neal Wang picture Neal Wang · Dec 10, 2011 · Viewed 229.9k times · Source

How would you convert a string to ASCII values?

For example, "hi" would return 104105.

I can individually do ord('h') and ord('i'), but it's going to be troublesome when there are a lot of letters.

Answer

Mark Byers picture Mark Byers · Dec 10, 2011

You can use a list comprehension:

>>> s = 'hi'
>>> [ord(c) for c in s]
[104, 105]