首页 > 编程知识 正文

Python 程序:查找字符的 ASCII 值

时间:2023-11-24 15:33:39 阅读:308862 作者:KONN

用例子写一个 Python 程序来查找一个字符的 ASCII 值。在 Python 中,每个字符都有自己的 ASCII 值(只有一个整数)。例如,A 的 ASCII 值是 65。

Python 程序查找字符的 ASCII 值示例 1

这个 python 程序返回 t 的 ASCII 值,请参考 Python 中的 ASCII 表一文,了解 ASCII 字符及其十进制、十六进制和八进制数字的列表。

# Python Program to find ASCII Value of a Character

ch = 'T'

print("The ASCII Value of %c = %d" %(ch, ord(ch)))
The ASCII Value of T = 84

显示字符 ASCII 值的 Python 程序示例 2

这个返回 ASCII 值的 Python 程序与上面相同。但是,这次我们允许用户输入自己的字符。

ch = input("Please enter any Character  :  ")

print("The ASCII Value of %c = %d" %(ch, ord(ch)))
Please enter any Character  :  a
The ASCII Value of a = 97

返回字符 ASCII 值的 Python 程序示例 3

如果你的用户输入了多个字符,那么上面的 python 程序返回错误。在这个程序中,我们使用索引位置来返回第一个字符的 ASCII 值。

ch = input("Please enter any Character  :  ")

print("The ASCII Value = %d" %ord(ch[0]))

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。