Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使大小写不敏感?_Python_Case Insensitive - Fatal编程技术网

Python 如何使大小写不敏感?

Python 如何使大小写不敏感?,python,case-insensitive,Python,Case Insensitive,如何使输入不区分大小写 int_string = input("What is the initial string? ") int_string = int_string.lower() 如果您不喜欢所有重复的代码,您可以使用total_ordering来填写一些这样的方法 class CaseInsensitiveStr(str): def __eq__(self, other): return str.__eq__(self.lower(), other.lowe

如何使输入不区分大小写

int_string = input("What is the initial string? ")
int_string = int_string.lower()
如果您不喜欢所有重复的代码,您可以使用total_ordering来填写一些这样的方法

class CaseInsensitiveStr(str):
    def __eq__(self, other):
        return str.__eq__(self.lower(), other.lower())
    def __ne__(self, other):
        return str.__ne__(self.lower(), other.lower())
    def __lt__(self, other):
        return str.__lt__(self.lower(), other.lower())
    def __gt__(self, other):
        return str.__gt__(self.lower(), other.lower())
    def __le__(self, other):
        return str.__le__(self.lower(), other.lower())
    def __ge__(self, other):
        return str.__ge__(self.lower(), other.lower())

int_string = CaseInsensitiveStr(input("What is the initial string? "))
测试用例:

from functools import total_ordering

@total_ordering
class CaseInsensitiveMixin(object):
    def __eq__(self, other):
        return str.__eq__(self.lower(), other.lower())
    def __lt__(self, other):
        return str.__lt__(self.lower(), other.lower())

class CaseInsensitiveStr(CaseInsensitiveMixin, str):
    pass
问题是由于所述的输入功能引起的

此函数不捕获用户错误。如果输入不正确 如果语法有效,将引发语法错误。其他例外情况 如果评估过程中出现错误,可能会引发

考虑使用raw_输入函数进行用户的一般输入


因此,只要使用原始输入,一切都可以正常运行

Python?如果是这样,请添加标记-同时,解释什么不起作用。是的,python,不区分大小写什么不区分大小写?区分大小写仅在比较时适用,并且在所提供的代码中没有比较。我尝试将用户输入的任何内容都区分为大小写insensitive@pst你是什么意思?str.lower工作正常,str.lowercase不存在。该文档是针对字符串模块的,而不是str类型
s = CaseInsensitiveStr("Foo")
assert s == "foo"
assert s == "FOO"
assert s > "bar"
assert s > "BAR"
assert s < "ZAB"
assert s < "ZAB"