Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python3 chr() Function

The Python chr() function

Python3 Built-in Functions Python3 built-in 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

- Type: integer (Unicode code point, 0 to 1,114,111) - Description: the Unicode code point to convert to a character.

Function description


Example

Example 1: basic usage

Example

Basic English characters

print

(

chr

(

65

)

)

 

Output: A

print

(

chr

(

90

)

)

 

Output: Z

print

(

chr

(

97

)

)

 

Output: a

print

(

chr

(

122

)

)

 

Output: z

Digit characters

print

(

chr

(

48

)

)

 

Output: 0

print

(

chr

(

57

)

)

 

Output: 9

Common symbols

print

(

chr

(

33

)

)

 

Output: !

print

(

chr

(

64

)

)

 

Output: @

print

(

chr

(

32

)

)

 

Output: space

Chinese

print

(

chr

(

20013

)

)

 

Output: Zhong

print

(

chr

(

25991

)

)

 

Output: Wen

Expected output:

A
Z
a
z
0
9
!
@

中
文

Code analysis:

  1. 65-90 are the Unicode code points of uppercase letters A-Z.
  2. 97-122 are the Unicode code points of lowercase letters a-z.
  3. 48-57 are the Unicode code points of digits 0-9.

Example 2: used together with ord()

Example

ord() is the inverse of chr()

print

(

ord

(

'A'

)

)

   

Output: 65

print

(

chr

(

65

)

)

   

Output: A

print

(

chr

(

ord

(

'A'

)

1

)

)

 

Output: B

Iterate over uppercase letters

for

i

in

range

(

65

,

91

)

:

   

print

(

chr

(

i

)

,

end

=

" "

)

print

(

)

 

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

)

print

(

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 Functions Python3 built-in function