The Python chr() function
chr() is a built-in Python function that converts an integer to the corresponding character.
chr() It takes a Unicode code point (an integer) and returns the corresponding character. It is the inverse of ord() .
Word meaning: chr Is characteris short for (character).
Basic syntax and parameters
Syntax format
chr(i)
Parameter Description
- Parameter i:
- Type: integer (Unicode code point, 0 to 1,114,111)
- Description: the Unicode code point to convert to a character.
Function description
- Return Value: returns a string of length 1, i.e. the corresponding character.
- Range: the valid Unicode code point range is 0 to 1,114,111 (0x10FFFF).
Example
Example 1: basic usage
Example
Basic English characters
(
chr
(
65
)
)
Output: A
(
chr
(
90
)
)
Output: Z
(
chr
(
97
)
)
Output: a
(
chr
(
122
)
)
Output: z
Digit characters
(
chr
(
48
)
)
Output: 0
(
chr
(
57
)
)
Output: 9
Common symbols
(
chr
(
33
)
)
Output: !
(
chr
(
64
)
)
Output: @
(
chr
(
32
)
)
Output: space
Chinese
(
chr
(
20013
)
)
Output: Zhong
(
chr
(
25991
)
)
Output: Wen
Expected output:
A
Z
a
z
0
9
!
@
中
文
Code analysis:
- 65-90 are the Unicode code points of uppercase letters A-Z.
- 97-122 are the Unicode code points of lowercase letters a-z.
- 48-57 are the Unicode code points of digits 0-9.
Example 2: used together with ord()
Example
ord() is the inverse of chr()
(
ord
(
'A'
)
)
Output: 65
(
chr
(
65
)
)
Output: A
(
chr
(
ord
(
'A'
)
1
)
)
Output: B
Iterate over uppercase letters
for
i
in
range
(
65
,
91
)
:
(
chr
(
i
)
,
end
=
" "
)
(
)
Output: A B C ... Z
Generate character art
pattern
=
[
]
for
i
in
range
(
0
,
256
,
16
)
:
row
=
''
.
join
(
chr
(
j
)
for
j
in
range
(
i
,
min
(
i+
16
,
256
)
)
)
pattern.
append
(
row
)
(
pattern
[
0
]
)
Output: !"#$%&'()*+,-./
Expected output:
65
A
B
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
!"#$%&'()*+,-./
chr() and ord() are inverse functions, often used together to handle character encoding.
Python3 built-in function