412

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

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

DigviJay Patil
  • 986
  • 14
  • 31
Manuel Araoz
  • 15,962
  • 24
  • 71
  • 95

5 Answers5

707

Use chr() and ord():

>>> chr(97)
'a'
>>> ord('a')
97
Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
139
>>> ord('a')
97
>>> chr(97)
'a'
dwc
  • 24,196
  • 7
  • 44
  • 55
22

ord and chr

rmmh
  • 6,997
  • 26
  • 37
2

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')
Alec
  • 8,529
  • 8
  • 37
  • 63
1

For long string you could use this.

 ''.join(map(str, map(ord, 'pantente')))
Pjl
  • 1,752
  • 18
  • 21