如何让 Python 识别小写和大写输入?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12719586/
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
How to let Python recognize both lower and uppercase input?
提问by user1718826
I am new to Python. I am writing a program that distinguishes whether or not a word starts with a vowel. The problem is, that the program is only able to correctly handle uppercase letters as input. For example, if I provide the word "Apple" as input, the result is True; however, if the word "apple" is provided as input, the result is False. How do I fix it?
我是 Python 的新手。我正在编写一个程序来区分一个单词是否以元音开头。问题是,该程序只能正确处理大写字母作为输入。例如,如果我提供单词“Apple”作为输入,结果是True;但是,如果提供单词“apple”作为输入,则结果为False。我如何解决它?
word = input ("Please Enter a word:")
if (word [1] =="A") :
print("The word begins with a vowel")
elif (word [1] == "E") :
print("The word begins with a vowel")
elif (word [1] == "I") :
print("The word begins with a vowel")
elif (word [1] == "O") :
print("The word begins with a vowel")
elif (word [1] == "U") :
print("The word begins with a vowel")
else:
print ("The word do not begin with a vowel")
回答by Blender
Convert the word entirely to lowercase (or uppercase) first:
首先将单词完全转换为小写(或大写):
word = input("Please Enter a word:").lower() # Or `.upper()`
Also, to get the first letter of your word, use word[0], not word[1]. Lists are zero-indexed in Python and almost all programming languages.
另外,要获取单词的第一个字母,请使用word[0],而不是word[1]。列表在 Python 和几乎所有编程语言中都是零索引的。
You can also condense your code by quite a bit:
您还可以大量压缩您的代码:
word = input("Please Enter a word:")
if word[0].lower() in 'aeiou':
print("The word begins with a vowel")
else:
print("The word do not begin with a vowel")
回答by Matt Ball
You could convert the input to upper case before comparing.
您可以在比较之前将输入转换为大写。
回答by Harrt
You should use:
你应该使用:
word[i] in 'AEIOUaeiou'
回答by John La Rooy
Usually you would use str.lower()(or str.upper()) on the input to normalise it.
通常你会在输入上使用str.lower()(或str.upper()) 来规范化它。
Python3.3 has a new method called str.casefold()which works properly for unicode
Python3.3 有一个新方法调用str.casefold(),它适用于 unicode
回答by Marwan Alsabbagh
The check for vowels is done using str.startswithwhich can accept a tuple of multiple values. PEP 8 Style Guide for Python Coderecommends the use of startswith with over string slicing for better readability of code:
元音检查是使用str.startswith完成的,它可以接受多个值的元组。PEP 8 Python 代码风格指南建议使用带有过度字符串切片的 startswith 以提高代码的可读性:
Use ''.startswith() and ''.endswith() instead of string slicing to check for prefixes or suffixes.
使用 ''.startswith() 和 ''.endswith() 而不是字符串切片来检查前缀或后缀。
Conditional Expressionsare used to set the message indicating whether the word starts with a vowel or not. Then I used the String Formattingmethod to prepare the message. Also just as a English grammar correction thing I replaced the sentence "The word do not begin with a vowel" with "The word does not begin with a vowel".
条件表达式用于设置指示单词是否以元音开头的消息。然后我使用String Formatting方法来准备消息。同样作为英语语法更正,我将句子“单词不以元音开头”替换为“单词不以元音开头”。
word = input("Please Enter a word:")
is_vowel = 'does' if word.lower().startswith(tuple('aeiou')) else 'does not'
print("The word {} begin with a vowel".format(is_vowel))
回答by Kapil Marwaha
1) There are many ways to do what needs to be done, like what Blender said. However, what you are trying to do is to convert the first letter to upper whether the input is upper or lower. Use 'capitalize' to do this.
1)有很多方法可以做需要做的事情,就像Blender所说的那样。但是,您要做的是将第一个字母转换为大写,无论输入是大写还是小写。使用“大写”来做到这一点。
2) You also need to use word[0], instead of word[1] to get the first letter out
2)您还需要使用word[0],而不是word[1]来取出第一个字母
word = raw_input("Please Enter a word: ").capitalize()
if word [0] in "AEIOU" :
print("The word begins with a vowel")
else:
print ("The word does not begin with a vowel")
This will make the first letter in upper case and the rest will remain as is.
这将使第一个字母大写,其余的将保持原样。

