Case-insensitive string equality

MRAB python at mrabarnett.plus.com
Thu Aug 31 19:53:38 EDT 2017


On 2017-08-31 16:29, Tim Chase wrote:
> On 2017-08-31 07:10, Steven D'Aprano wrote:
>> So I'd like to propose some additions to 3.7 or 3.8.
> 
> Adding my "yes, a case-insensitive equality-check would be useful"
> with the following concerns:
> 
> I'd want to have an optional parameter to take locale into
> consideration.  E.g.
> 
>    "i".case_insensitive_equals("I") # depends on Locale
>    "i".case_insensitive_equals("I", Locale("TR")) == False
>    "i".case_insensitive_equals("I", Locale("US")) == True
> 
> and other oddities like
> 
>    "ß".case_insensitive_equals("SS") == True
> 
> (though casefold() takes care of that later one).  Then you get
> things like
> 
>    "III".case_insensitive_equals("\N{ROMAN NUMERAL THREE}")
>    "iii".case_insensitive_equals("\N{ROMAN NUMERAL THREE}")
>    "FI".case_insensitive_equals("\N{LATIN SMALL LIGATURE FI}")
> 
> where the decomposition might need to be considered.  There are just
> a lot of odd edge-cases to consider when discussing fuzzy equality.
> 
>> (1) Add a new string method,
> 
> This is my preferred avenue.
> 
>> Alternatively: how about a === triple-equals operator to do the
>> same thing?
> 
> No.  A strong -1 for new operators.  This peeves me in other
> languages (looking at you, PHP & JavaScript)
> 
>> (2) Add keyword-only arguments to str.find and str.index:
>> 
>>     casefold=False
>> 
>>     which does nothing if false (the default), and switches to a
>> case- insensitive search if true.
> 
> I'm okay with some means of conveying the insensitivity to
> str.find/str.index but have no interest in list.find/list.index
> growing similar functionality.  I'm meh on the "casefold=False"
> syntax, especially in light of my hope it would take a locale for the
> comparisons.
> 
[snip]
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)



More information about the Python-list mailing list