439

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

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

5 Answers 5

748

Use chr() and ord():

>>> chr(97) 'a' >>> ord('a') 97 
Sign up to request clarification or add additional context in comments.

Comments

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

4 Comments

You posted this answer 1 minute before the other guy yet still missed out on the 500+ upvotes...
that is because the latter got selected as the answer lol
Such is life. I upvoted this one as well, but I doubt he still cares 11 years later.
the latter one has documentation links)
22

ord and chr

2 Comments

My favorite part about this answer is that they inadvertently wrote a valid line of Python.
print ((ord and chr)(0O041))
7

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') 

Comments

1

For long string you could use this.

 ''.join(map(str, map(ord, 'pantente'))) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.