Question

How do I make this code case.insensitive? Suggestions?

def not3(string2, string1):
    if len(string2) < 3:  return True
    if string2[:3] in string1: return False
    return not3(string2[1:], string1)
Was it helpful?

Solution

Lowercase the in operands:

if string2[:3].lower() in string1.lower(): return False

The len() test is not influenced by case.

OTHER TIPS

Usually, you would probably want to lowercase the input before you send it to the function:

>>> not3('abc', 'ABCD')
True
>>> not3('abc'.lower(), 'ABCD'.lower())
False

This way, you can use the same function in a case-senstive or a case-insensitive context.

You can also make a case-insensitive version of your function like this:

def not3_case_insensitive(string2, string1):
    return not3(string2.lower(), string1.lower())
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top