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.

Asked By: Manuel Araoz

||

Answers:

ord and chr

Answered By: rmmh
>>> ord('a')
97
>>> chr(97)
'a'
Answered By: dwc

Use chr() and ord():

>>> chr(97)
'a'
>>> ord('a')
97
Answered By: Adam Rosenfield

For long string you could use this.

 ''.join(map(str, map(ord, 'pantente')))
Answered By: Pjl

Not OP’s question, but given the title How can I convert a character to a integer in Python,

int(num) <==> ord(num) - ord('0')

str(char) <==> ord(char) - ord('a')
Answered By: Alec