Search code examples
pythonstringascii

Convert string to ASCII value python


How would you convert a string to ASCII values?

For example, "hi" would return [104 105].

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


Solution

  • You can use a list comprehension:

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