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


Question

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

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

1
255
6/10/2015 1:40:37 PM

Accepted Answer

Use chr() and ord():

>>> chr(97)
'a'
>>> ord('a')
97
453
4/1/2009 5:22:00 AM

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

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon