【问题标题】:Printing the unicode of all the letters in a word [duplicate]打印一个单词中所有字母的unicode [重复]
【发布时间】:2019-06-02 14:53:28
【问题描述】:

我被这个问题困住了:

用户输入一个单词,程序输出单词中每个字母的unicode

输入语句的外观如下:

word = input("Enter a word: ")

假设用户输入单词“Cat”,输出将如下所示:

C: 67
a: 97
t: 116

【问题讨论】:

  • 虽然您已经给出了输出示例,但它与描述 Unicode 字符串的方式并不一致。我建议像这样U+0043U+0061U+0074。这样就很清楚输入是什么,问题减少到迭代 Unicode 代码点(在数字上与 UTF-32 代码单元相同,如果使用 Python

标签: python python-unicode


【解决方案1】:

使用ord()。

word = input("Enter a word: ")
for letter in word:
    print(letter + ': ', ord(letter))

【讨论】:

    【解决方案2】:

    根据您的示例,我猜您尝试输出的是字符的 ASCII 值。如果是这样,您可以使用 python 内置函数 ord() 检查 ASCII 值。

    userInput = input("Enter a word: ")
    
    for i in userInput:
        print('{}: {}'.format(i, ord(i)))
    

    输出:

    C: 67
    a: 97
    t: 116
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-17
      • 2019-01-27
      • 1970-01-01
      相关资源
      最近更新 更多