Home > Article > Backend Development > How to get the ASCII code of a character
How to get the ASCII code of a character
##Python built-in ord function
Used to convert characters into ASCII codesUsage is:print('The ASCII code of character 0 is represented as',ord('0'))The result is : The ASCII code of character 0 is represented as 48Python built-in chr function
is used to convert ASCII code into characters Usage is: print('The character corresponding to ASCII code 97 is',chr(97))The result is: The character corresponding to ASCII code 97 is aCase:
# Filename : test.py # 用户输入字符 c = input("请输入一个字符: ") # 用户输入ASCII码,并将输入的数字转为整型 a = int(input("请输入一个ASCII码: ")) print( c + " 的ASCII 码为", ord(c)) print( a , " 对应的字符为", chr(a))
The results are as follows
python3 test.py 请输入一个字符: a 请输入一个ASCII码: 101 a 的ASCII 码为 97 101 对应的字符为 e
The above is the detailed content of How to get the ASCII code of a character. For more information, please follow other related articles on the PHP Chinese website!