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

Python Convert Between ASCII Codes and Characters

Python Convert Between ASCII Codes and Characters

Document Object Reference Manual Python3 Examples

The following code converts between ASCII codes and characters:

Example (Python 3.0+)

Filename : test.py

author by : www.runoob.com

The user enters a character

c

=

input

(

"

Please enter a character:

"

)

The user enters an ASCII code, and the entered number is converted to an integer

a

=

int

(

input

(

"

Please enter an ASCII code:

"

)

)

print

(

c

"

The ASCII code of is

"

,

ord

(

c

)

)

print

(

a

,

"

The corresponding character is

"

,

chr

(

a

)

)

The output of the code above is:


python3 test.py 
请输入一个字符: a
请输入一个ASCII码: 101
a 的ASCII 码为 97
101  对应的字符为 e

Document Object Reference Manual Python3 Examples