如何比较不区分大小写的字符串

时间:2019-01-12 20:39:59

标签: python-3.x text case-sensitive case-insensitive

所以我正在尝试制作字典应用。我希望它不区分大小写。首先,我看到了一些解决此问题的方法,但都不适合我。让我用一个例子来解释它:假设我有一个School这个词,当我像School一样搜索它时,代码可以正常工作,但是当我像school那样搜索时,它就无法工作。

我真的没有得到这个解决方案https://stackoverflow.com/a/15400311/9692934

    key_to_search = input() #not raw_input() since its python 3    
    with open("fileOfWords.txt") as enter:
        for line in enter:
            if line.startswith("%s" % key_to_search):
                print(key_to_search + " is in the dictionary")

我希望School等于schoolscHoolschooL。但在我的情况下,School等于School

2 个答案:

答案 0 :(得分:0)

如果您希望不区分大小写,只需将输入和行转换为小写并进行比较即可。

ALTER TABLE badv2018
ADD New AS CAST([BB Percent] / 100.0 AS DECIMAL(4, 3));

答案 1 :(得分:0)

我认为您可以简单地在搜索字符串lower上调用key_to_search = key_to_search.lower()并将所有单词保存为小写。您的代码将是:

key_to_search = input().lower() #not raw_input() since its python 3    
    with open("fileOfWords.txt") as enter:
        for line in enter:
            if line.startswith("%s" % key_to_search):
                print(key_to_search + " is in the dictionary")