【问题标题】:How do I check if something is a letter or not?我如何检查某个东西是否是字母?
【发布时间】:2022-08-24 18:26:28
【问题描述】:

我想检查某人输入的信息是否是字母

a=input(\"Input one letter: \")
#This doesn\'t actually work
if a==letter:
  print(\"wow u inputted a letter\")
else:
  print(\"not a letter\")

另外,我不想写 26 个 if 语句

    标签: python input letter


    【解决方案1】:

    字符串库中有一个名为ascii_letters 的常量:

    from string import ascii_letters # 'abc...xyzABC...XYZ'
    
    a = input("Input one letter: ")
    if a in list(ascii_letters):
        print('This is a letter')
    else:
        print('This is not a letter')
    

    【讨论】:

      猜你喜欢
      • 2012-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-12
      • 1970-01-01
      • 1970-01-01
      • 2010-12-18
      相关资源
      最近更新 更多