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

PythonIntegerCharType Conversion

Python Problem Overview


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

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

Python Solutions


Solution 1 - Python

Use chr() and ord():

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

Solution 2 - Python

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

Solution 3 - Python

ord and chr

Solution 4 - Python

For long string you could use this.

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionManuel AraozView Question on Stackoverflow
Solution 1 - PythonAdam RosenfieldView Answer on Stackoverflow
Solution 2 - PythondwcView Answer on Stackoverflow
Solution 3 - PythonrmmhView Answer on Stackoverflow
Solution 4 - PythonPjlView Answer on Stackoverflow