Python - 忽略字母大小写

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35599982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 16:39:31  来源:igfitidea点击:

Python - Ignore letter case

python

提问by Hyman Anyon

I have an if statement:

我有一个 if 语句:

rules = input ("Would you like to read the instructions? ")
rulesa = "Yes"
if rules == rulesa:
    print  ("No cheating")
else: print ("Have fun!")

I wish for the user to be able to answer with Yes, YES, yES, yes or any other capitalisation, and for the code to know that they mean Yes.

我希望用户能够用 Yes、YES、yES、yes 或任何其他大写字母来回答,并且让代码知道它们的意思是 Yes。

采纳答案by Nikolai Saiko

For this simple example you can just compare lowercased ruleswith "yes":

对于这个简单的示例,您可以将小写rules与进行比较"yes"

rules = input ("Would you like to read the instructions? ")
rulesa = "yes"
if rules.lower() == rulesa:
    print  ("No cheating")
else: 
    print ("Have fun!")

It is OK for many cases, but be awared, some languages may give you a tricky results. For example, German letter ?gives following:

在许多情况下都可以,但请注意,某些语言可能会给您带来棘手的结果。例如,德语字母?给出以下内容:

"?".lower() is "?"
"?".upper() is "SS"
"?".upper().lower() is "ss"
("?".upper().lower() == "?".lower()) is False

So we may have troubles, if our string was uppercased somewhere before our call to lower(). Same behaviour may also be met in Greek language. Read the post https://stackoverflow.com/a/29247821/2433843for more information.

所以我们可能会遇到麻烦,如果我们的字符串在调用lower(). 在希腊语中也可能遇到相同的行为。阅读帖子 https://stackoverflow.com/a/29247821/2433843了解更多信息。

So in generic case, you may need to use str.casefold()function (since python3.3), which handles tricky cases and is recommended way for case-independent comparation:

因此,在一般情况下,您可能需要使用str.casefold()函数(自 python3.3 起),它处理棘手的情况,并且是独立于案例的比较的推荐方法

rules.casefold() == rulesa.casefold()

instead of just

而不仅仅是

rules.lower() == rulesa.lower()

回答by gtlambert

Use the following:

使用以下内容:

if rules.lower() == rulesa.lower():

This converts both strings to lower case before testing for equality.

这会在测试相等之前将两个字符串转换为小写。

回答by juanchopanza

A common approach is to make the input upper or lower case, and compare with an upper or lower case word:

一种常见的做法是将输入大写或小写,并与大写或小写单词进行比较:

rulesa = 'yes'
if rules.lower() == rulesa:
   # do stuff

回答by George Zachariadis

rules = raw_input("Would you like to read the instructions? ")
rulesa = "Yes"
if (rules == rulesa) or (rules.lower() == rulesa.lower()) or rules.upper() == rulesa.uppper():
   print  ("No cheating")
else: print ("Have fun!")

That will work everytime no matter the input and will keep the user's input as it was inputed! Have fun with it.

无论输入如何,这都会在每次输入时都有效,并且会保持用户输入的内容!玩得开心。

回答by Kunal Jain

You can make either of uppercase comparison or lowercase comparison.

您可以进行大写比较或小写比较。

For Example:

例如:

rulesa.upper() == rules.upper()

or

或者

rulesa.lower() == rules.lower()

both will give you output as true

两者都会给你输出为