snippetpythonCriticalCanonical
How to get the ASCII value of a character
Viewed 0 times
howasciithecharactervalueget
Problem
How do I get the ASCII value of a character as an
int in Python?Solution
From here:
The function
of the char. And in case you want to
convert back after playing with the
number, function
In Python 2, there was also the
In Python 3 you can use
ord() - Python 3.6.5rc1 documentation
ord() - Python 2.7.14 documentation
The function
ord() gets the int valueof 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
Code Snippets
>>> ord('a')
97
>>> chr(97)
'a'
>>> chr(ord('a') + 3)
'd'
>>>>>> unichr(97)
u'a'
>>> unichr(1234)
u'\u04d2'Context
Stack Overflow Q#227459, score: 1820
Revisions (0)
No revisions yet.