def find_first_occurrence(msg, string_to_find):
# looks like it wants you to not care about case, as 'H' returns 8 on an 'h'
string_to_find = string_to_find.lower();
msg = msg.lower();
return msg.find(string_to_find);
print find_first_occurrence("October holidays: Halloween and Thanksgiving", "H");
这可能是你想做的:
按答复编辑:
str.find(val)
返回val在str中的位置。它区分大小写,因此在“Hello,Hello”中,str.find("h")
将返回7,str.find("H")
将返回0。在这个问题表明它不应该通过给你一行
>>> find_first_occurrence('October holidays: Halloween and Thanksgiving', 'H') 8
来考虑大小写。在通常,
'H'
会得到第18位,因为H是国会大厦。但他们说你应该得到8号位,也就是小写,这意味着外壳不重要。在通过执行
string_to_find = string_to_find.lower();
和msg = msg.lower();
,可以一起删除大小写,这样它将在所有小写字符串中找到第一个小写字母。在相关问题 更多 >
编程相关推荐