word = "hello My name is Bob"
for i in word:
if i == "m":
print("There is an M")为什么不印两次,有两次?
发布于 2022-02-17 01:55:46
您必须执行i.lower()以小写为"M“
发布于 2022-02-17 02:00:39
Python是一种区分大小写的语言,所以"M“和"m”是不同的。因此,要通过忽略大小写来比较它们,需要将两边的大小写进行转换。下面的代码将给出结果,要么是"M“,要么是"m”,例如:
word = "hello My name is Bob"
for i in word:
if i.lower() == "m".lower():
print("There is an M")发布于 2022-02-17 02:12:31
试着粘贴这个:
word = "hello My name is Bob"
for i in word.lower():
if i.lower() == "m":
print("There is an M")输出的问题是Python的大小写敏感性。Python读取'word‘变量,只找到一个’m‘(简单的m)。所以它只印了一次。通过添加'.lower()',我们将整个字符串转换为通向预期输出的简单字母。
https://stackoverflow.com/questions/71151541
复制相似问题