How to Convert int to a char in Python
There are two main ways of converting an int into a char in Python. If you want the output to interpreted as an ASCII value, use the chr()
Python function and for a numerical representation use the Python str()
Function.
Integer to Char Using the chr() Function
Let's convert an int into its ASCII representation using chr()
.
string = chr(97)
print(string)
a
The ASCII representation of 97 is a
, hence the output we can see in the example above.
Convert an Int to a String
To convert an int into a string, use the native Python str()
function.
string = str(97)
print(string)
97
string = str(97)
print(string)
print(type(string))
97
<class 'str'>