1294

How do I get the ASCII value of a character as an int in Python?

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Matt
  • 84,419
  • 25
  • 57
  • 67

5 Answers5

1712

From here:

The function ord() gets the int value of the char. And in case you want to convert back after playing with the number, function chr() does the trick.

>>> ord('a')
97
>>> chr(97)
'a'
>>> chr(ord('a') + 3)
'd'
>>>

In Python 2, there was also the unichr function, returning the Unicode character whose ordinal is the unichr argument:

>>> unichr(97)
u'a'
>>> unichr(1234)
u'\u04d2'

In Python 3 you can use chr instead of unichr.


ord() - Python 3.6.5rc1 documentation

ord() - Python 2.7.14 documentation

smci
  • 32,567
  • 20
  • 113
  • 146
Matt J
  • 43,589
  • 7
  • 49
  • 57
  • which encoding in chr using ? – njzk2 Dec 14 '11 at 08:59
  • @njzk2: `latin1` (which is not a brilliant thing to do if your original byte was encoded in (say) `cp1251` (Cyrillic) – John Machin Apr 17 '12 at 04:57
  • 23
    Note that chr also acts as unichr in Python 3. `chr(31415) -> '窷'` – William Apr 03 '13 at 13:47
  • 7
    @njzk2: it doesn't use any character encoding it returns a bytestring in Python 2. It is upto you to interpret it as a character e.g., `chr(ord(u'й'.encode('cp1251'))).decode('cp1251') == u'й'`. In Python 3 (or `unichr` in Python 2), the input number is interpreted as Unicode codepoint integer ordinal: `unichr(0x439) == '\u0439'` (the first 256 integers has the same mapping as latin-1: `unichr(0xe9) == b'\xe9'.decode('latin-1')`, the first 128 -- ascii: `unichr(0x0a) == b'\x0a'.decode('ascii')` it is a Unicode thing, not Python). – jfs Apr 30 '14 at 02:59
  • 6
    Why is the function called "ord"? – eLymar Aug 01 '18 at 17:10
  • 11
    @eLymar: it's short for "ordinal," which has similar linguistic roots to "order" - i.e. the numeric rather than symbolic representation of the character – Jacob Krall Jan 16 '19 at 16:30
187

Note that ord() doesn't give you the ASCII value per se; it gives you the numeric value of the character in whatever encoding it's in. Therefore the result of ord('ä') can be 228 if you're using Latin-1, or it can raise a TypeError if you're using UTF-8. It can even return the Unicode codepoint instead if you pass it a unicode:

>>> ord(u'あ')
12354
Xantium
  • 11,201
  • 10
  • 62
  • 89
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • 17
    How can you find out which encoding you are using in a given situation? – Moustache Mar 15 '17 at 17:08
  • 3
    @Moustache : In Python3, you'll be using Unicode out-of-the-box. – tricasse Oct 01 '19 at 09:33
  • 2
    Depends on the **object type**. Python3 (**str**): `unicode` by default. Python3 (**bytes**): `str(b'\xc3\x9c', 'ascii')` -> raises _UnicodeDecodeError_. Python3 (**bytes**): `str(b'\xc3\x9c', 'utf-8')` -> returns _Ü_. You can also look into the [six](https://six.readthedocs.io/) package. – nosahama Apr 23 '20 at 11:40
64

You are looking for:

ord()
Jacob Krall
  • 28,341
  • 6
  • 66
  • 76
46

The accepted answer is correct, but there is a more clever/efficient way to do this if you need to convert a whole bunch of ASCII characters to their ASCII codes at once. Instead of doing:

for ch in mystr:
    code = ord(ch)

or the slightly faster:

for code in map(ord, mystr):

you convert to Python native types that iterate the codes directly. On Python 3, it's trivial:

for code in mystr.encode('ascii'):

and on Python 2.6/2.7, it's only slightly more involved because it doesn't have a Py3 style bytes object (bytes is an alias for str, which iterates by character), but they do have bytearray:

# If mystr is definitely str, not unicode
for code in bytearray(mystr):

# If mystr could be either str or unicode
for code in bytearray(mystr, 'ascii'):

Encoding as a type that natively iterates by ordinal means the conversion goes much faster; in local tests on both Py2.7 and Py3.5, iterating a str to get its ASCII codes using map(ord, mystr) starts off taking about twice as long for a len 10 str than using bytearray(mystr) on Py2 or mystr.encode('ascii') on Py3, and as the str gets longer, the multiplier paid for map(ord, mystr) rises to ~6.5x-7x.

The only downside is that the conversion is all at once, so your first result might take a little longer, and a truly enormous str would have a proportionately large temporary bytes/bytearray, but unless this forces you into page thrashing, this isn't likely to matter.

Isma
  • 14,604
  • 5
  • 37
  • 51
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
7

To get the ASCII code of a character, you can use the ord() function.

Here is an example code:

value = input("Your value here: ")
list=[ord(ch) for ch in value]
print(list)

Output:

Your value here: qwerty
[113, 119, 101, 114, 116, 121]
Indi
  • 421
  • 9
  • 28