How can I convert a character to a integer in Python, and viceversa?

Manuel Araoz picture Manuel Araoz · Apr 1, 2009 · Viewed 514.7k times · Source

I want to get, given a character, its ASCII value.

For example, for the character a, I want to get 97, and vice versa.

Answer

Adam Rosenfield picture Adam Rosenfield · Apr 1, 2009

Use chr() and ord():

>>> chr(97)
'a'
>>> ord('a')
97