【问题标题】:How to ignorecase for input string [duplicate]如何忽略输入字符串的大小写[重复]
【发布时间】:2018-12-04 19:30:30
【问题描述】:

这是我正在学习的示例代码。我想获取一个字符串(1 个或多个字符)并将其与现有字符串进行比较。我想输入 blue 仍然得到答案。如何完全忽略此案? 谢谢。

parrot = "Norwegian Blue"

letter = input("Enter a character: ")

if letter in parrot:
    print("The letter {0} is in the Norwegian Blue".format(letter))
else:
    print("Sorry the letter is missing ")

成功案例:

C:\PythonMasterClass>python IfProgramFlow.py  
Enter a character: Blue  
The letter Blue is in the Norwegian Blue

对于失败的案例:

C:\PythonMasterClass>python IfProgramFlow.py  
Enter a character: blue  
Sorry the letter is missing

我检查了其他线程以将我的输入和实际文本转换为小写并进行比较,但我想要一个直接的方法。 如果我有多个字符串,我不想为此目的将所有字符串都保存为小写。 谢谢。

【问题讨论】:

    标签: python string substring ignore-case


    【解决方案1】:

    试试:if letter.lower() in parrot.lower():

    【讨论】:

    • 今天早上看到,我只是想用一种像 Java 一样的直接方法来匹配字符串。谢谢。
    猜你喜欢
    • 2012-12-10
    • 2010-09-08
    • 1970-01-01
    • 2012-01-21
    • 2019-08-29
    • 2013-05-12
    • 1970-01-01
    • 2015-09-12
    • 2015-12-03
    相关资源
    最近更新 更多