Char or character is a popular variable type used in different programming languages. Python also provides the ability to store characters in variables. But python does not provide a native character type. Instead, the string type is provided. There is also the chr() method which is used to convert provided Unicode into the character for string type.
chr() Method Syntax
The chr() method has the following simple syntax.
chr(CODE)
- CODE is the number representation of the ASCII or UTF character.
The chr() method returns the ASCII or UTF representation of the specified CODE.
Convert ASCII To Character
The ASCII table is used to give numbers for different characters. Also different special characters like space, null, tab etc expressed in ASCII table. For example the lowercase “a” is coded as 97 in ASCII table. The chr() method can be used to return the provided code character representation as string type.
a = chr(97)
b = chr(98)
c = chr(99)
d = chr(100)
print(a)
print(b)
print(c)
print(d)
a b c d
Print Control Characters
As stated previously the chr() method converts provided code into ASCII character which can be a letter or control character like tab, space etc. For example ASCII code 32 is related with the space. So the chr() method can be used to print space by using the 32.
print("I",chr(32),"like",chr(32),"pythontect.com")