Skip to main content
 首页 » 编程设计

python之不区分大小写的用户输入字符串

2025年05月04日17jirigala

使用哪个函数使用户输入的字符串不区分大小写?

correctAnswer = "London" 
 
userGuess = input("What is the capital of  Great Britain?: ") 
 
if userGuess == "London": 
    print("Correct!") 
else: 
    print("Wrong") 

我在字符串后面尝试了以下函数:

.lower() 
.capitalize() 
.casefold() 

输出仍然是“错误的”,尽管当输入为:
时答案应该是正确的 - 伦敦
- 伦敦
- 伦敦

等等..

请您参考如下方法:

在您的字符串比较中,正确答案本身的第一个字母大写。

correctAnswer = "london" 
 
userGuess = input("What is the capital of  Great Britain?: ").lower() 
 
if userGuess == correctAnswer: 
    print("Correct!") 
else: 
    print("Wrong")