首页 > 编程知识 正文

Python 程序:查找字符串中所有字符的 ASCII 值

时间:2023-11-24 15:33:39 阅读:308865 作者:DUXI

写一个 Python 程序,用一个实际的例子找到字符串中总字符的 ASCII 值。

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

这个 python 程序允许用户输入一个字符串。接下来,它使用 For 循环打印该字符串中的字符。这里,我们使用 For 循环来迭代字符串中的每个字符。在 For 循环中,我们使用 print 函数返回该字符串中所有字符的 ASCII 值。

提示:请参考 String 文章了解 String 的一切。也可以参考 Python 中的 ASCII 表一文来理解 ASCII 值。

# Python program to find ASCII Values of Total Characters in a String

str1 = input("Please Enter your Own String : ")

for i in range(len(str1)):
    print("The ASCII Value of Character %c = %d" %(str1[i], ord(str1[i])))

Python 程序获取字符串中的字符总数的 ASCII 值示例 2

这个 ASCII 值 python 程序同上。然而,我们只是将换成了同时换成了。

# Python program to find ASCII Values of Total Characters in a String

str1 = input("Please Enter your Own String : ")
i = 0

while(i < len(str1)):
    print("The ASCII Value of Character %c = %d" %(str1[i], ord(str1[i])))
    i = i + 1

Python 字符串 ASCII 值输出

Please Enter your Own String : Hello
The ASCII Value of Character H = 72
The ASCII Value of Character e = 101
The ASCII Value of Character l = 108
The ASCII Value of Character l = 108
The ASCII Value of Character o = 111

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