Home  >  Article  >  Backend Development  >  How to get the ASCII code of a character

How to get the ASCII code of a character

anonymity
anonymityOriginal
2019-05-25 11:14:1619262browse

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 codes

Usage 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 48

Python 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 a

Case:

# 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn