How to match a substring in a string, ignoring case

后端 未结 8 1760
既然无缘
既然无缘 2020-12-08 12:58

I\'m looking for ignore case string comparison in Python.

I tried with:

if line.find(\'mandy\') >= 0:

but no success for ignore

相关标签:
8条回答
  • 2020-12-08 13:13
    a = "MandY"
    alow = a.lower()
    if "mandy" in alow:
        print "true"
    

    work around

    0 讨论(0)
  • 2020-12-08 13:17

    you can also use: s.lower() in str.lower()

    0 讨论(0)
  • 2020-12-08 13:18

    If you don't want to use str.lower(), you can use a regular expression:

    import re
    
    if re.search('mandy', 'Mandy Pande', re.IGNORECASE):
        # Is True
    
    0 讨论(0)
  • 2020-12-08 13:23

    See this.

    In [14]: re.match("mandy", "MaNdY", re.IGNORECASE)
    Out[14]: <_sre.SRE_Match object at 0x23a08b8>
    
    0 讨论(0)
  • 2020-12-08 13:25

    You can use in operator in conjunction with lower method of strings.

    if "mandy" in line.lower():

    0 讨论(0)
  • 2020-12-08 13:26

    Try:

    if haystackstr.lower().find(needlestr.lower()) != -1:
      # True
    
    0 讨论(0)
提交回复
热议问题