编写一个Python程序,通过实际示例查找字符串中所有字符的ASCII值。
此Python程序允许用户输入一个字符串。接下来,它使用For循环打印该字符串中的字符。在这里,我们使用For循环来迭代字符串中的每个字符。在For循环内部,我们使用print函数返回该字符串中所有字符的ASCII值。
提示:请参考字符串文章以了解有关字符串的所有信息。此外,请参考Python中的ASCII表文章以了解ASCII值。
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值程序与上述程序相同。但是,我们只是将For循环替换为While循环。
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
字符串字符的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