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.

Use chr() and ord():

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

ord and chr

2

For long string you could use this.

 ''.join(map(str, map(ord, 'pantente')))
Maya Lin-Takahashi

Maya Lin-Takahashi

Consumer Tech & Gadget Reviewer

Maya is a hardware enthusiast who tests and reviews smart home devices, smartphones, wearables, and audio gear. She focuses on practical consumer value and build quality.

Share this article
Twitter Facebook Pinterest