How do I do a case-insensitive string comparison?

前端 未结 9 2216
无人及你
无人及你 2020-11-21 07:46

How can I do case insensitive string comparison in Python?

I would like to encapsulate comparison of a regular strings to a repository string using in a very simple

9条回答
  •  面向向阳花
    2020-11-21 08:22

    def insenStringCompare(s1, s2):
        """ Method that takes two strings and returns True or False, based
            on if they are equal, regardless of case."""
        try:
            return s1.lower() == s2.lower()
        except AttributeError:
            print "Please only pass strings into this method."
            print "You passed a %s and %s" % (s1.__class__, s2.__class__)
    

提交回复
热议问题