The chr () function in Python

In Python, the chr () function returns a character (a string) from an integer that represents the unicode of the returned character.

In Python, the chr () function returns a character (a string) from an integer that represents the unicode of the returned character. So what is the syntax of chr () function, parameter and how to use chr ()? Let's find out together.

The chr () function syntax in Python

 chr (i) 

Parameters of chr ()

  1. Function chr () comes with a single parameter or an integer i
  2. Valid range of integers is from 0 to 1,114,111.

Return value from chr ()

The function chr () returns the value:

  1. A character (a string) with Unicode code is (i).
  2. If (i) is outside the valid range, the program will notify ValueError

For example:

 print (chr (97)) 
print (chr (65))
print (chr (1200))

Running the above code, the program will return the result:

 a 
A
Ұ

Try an example with (i) outside the program's allowable scope:

 print (chr (-1)) 

Immediately we have the result ValueError:

 Traceback (most recent call last): 
File "", line 1, print
print (chr (-1))
ValueError: chr () arg not in range (0x110000)

See more:

  1. Functions in Python
  2. Built-in Python functions
  3. More than 100 Python exercises have solutions (sample code)

Last lesson: callable () function in Python

Next lesson: classmethod () function in Python

4 ★ | 1 Vote