How to convert ASCII codes to characters in Python?

In Python, you can use the ord() function to convert a character to ASCII code and the chr() function to convert ASCII code to a character.

Here is an example code:

# 将字符转换为ASCII码
ch = 'A'
ascii_code = ord(ch)
print(ascii_code)

# 将ASCII码转换为字符
ascii_code = 65
ch = chr(ascii_code)
print(ch)

The output is:

65
A

In the above code, ord(ch) converts the character ‘A’ to ASCII code 65, while chr(ascii_code) converts ASCII code 65 back to the character ‘A’.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds