ChatGPT解决这个技术问题 Extra ChatGPT

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

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

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


A
Adam Rosenfield

Use chr() and ord():

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

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

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)
r
rmmh

ord and chr


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

For long string you could use this.

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