python – How to get the ASCII value of a character

python – How to get the ASCII value of a character

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)
ua
>>> unichr(1234)
uu04d2

In Python 3 you can use chr instead of unichr.


ord() – Python 3.6.5rc1 documentation

ord() – Python 2.7.14 documentation

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

>>> ord(uあ)
12354

python – How to get the ASCII value of a character

You are looking for:

ord()

Leave a Reply

Your email address will not be published. Required fields are marked *