Case-insensitive string equality

Steve D'Aprano steve+python at pearwood.info
Fri Sep 1 20:09:39 EDT 2017


On Sat, 2 Sep 2017 01:41 am, Chris Angelico wrote:

> On Fri, Sep 1, 2017 at 11:22 PM, Steve D'Aprano
> <steve+python at pearwood.info> wrote:
>> On Fri, 1 Sep 2017 09:53 am, MRAB wrote:
>>
>>> What would you expect the result would be for:
>>>
>>>      "\N{LATIN SMALL LIGATURE FI}".case_insensitive_find("F")
>>>
>>>      "\N{LATIN SMALL LIGATURE FI}".case_insensitive_find("I)
>>
>> That's easy.
>>
>> -1 in both cases, since neither "F" nor "I" is found in either string. We can
>> prove this by manually checking:
>>
>> py> for c in "\N{LATIN SMALL LIGATURE FI}":
>> ...     print(c, 'F' in c, 'f' in c)
>> ...     print(c, 'I' in c, 'i' in c)
>> ...
>> fi False False
>> fi False False
>>
>>
>> If you want some other result, then you're not talking about case
>> sensitivity.
> 
>>>> "\N{LATIN SMALL LIGATURE FI}".upper()
> 'FI'


The question wasn't what "\N{LATIN SMALL LIGATURE FI}".upper() would find,
but "\N{LATIN SMALL LIGATURE FI}".

Nor did they ask about

"\N{LATIN SMALL LIGATURE FI}".replace("\N{LATIN SMALL LIGATURE
FI}", "Surprise!")



> So what's the definition of "case insensitive find"? The most simple
> and obvious form is:
> 
> def case_insensitive_find(self, other):
>     return self.casefold().find(other.casefold())

That's not the definition, that's an implementation.


-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list