How do I do a case-insensitive string comparison?

前端 未结 9 2202
无人及你
无人及你 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:30

    This is another regex which I have learned to love/hate over the last week so usually import as (in this case yes) something that reflects how im feeling! make a normal function.... ask for input, then use ....something = re.compile(r'foo*|spam*', yes.I)...... re.I (yes.I below) is the same as IGNORECASE but you cant make as many mistakes writing it!

    You then search your message using regex's but honestly that should be a few pages in its own , but the point is that foo or spam are piped together and case is ignored. Then if either are found then lost_n_found would display one of them. if neither then lost_n_found is equal to None. If its not equal to none return the user_input in lower case using "return lost_n_found.lower()"

    This allows you to much more easily match up anything thats going to be case sensitive. Lastly (NCS) stands for "no one cares seriously...!" or not case sensitive....whichever

    if anyone has any questions get me on this..

        import re as yes
    
        def bar_or_spam():
    
            message = raw_input("\nEnter FoO for BaR or SpaM for EgGs (NCS): ") 
    
            message_in_coconut = yes.compile(r'foo*|spam*',  yes.I)
    
            lost_n_found = message_in_coconut.search(message).group()
    
            if lost_n_found != None:
                return lost_n_found.lower()
            else:
                print ("Make tea not love")
                return
    
        whatz_for_breakfast = bar_or_spam()
    
        if whatz_for_breakfast == foo:
            print ("BaR")
    
        elif whatz_for_breakfast == spam:
            print ("EgGs")
    

提交回复
热议问题