Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is `string.swapcase().swapcase()` not equal to `string`?

Documentation for str.swapcase() method says:

Return a copy of the string with uppercase characters converted to lowercase and vice versa. Note that it is not necessarily true that s.swapcase().swapcase() == s.

I can't think of an example where s.swapcase().swapcase() != s, can anyone think of one?

like image 322
Mr Matrix Avatar asked Mar 02 '23 12:03

Mr Matrix


2 Answers

A simple example would be:

s = "ß"

print(s.swapcase().swapcase())

Ouput:

ss

ß is German lowercase double s (The correct uppercase version would be ). The reason this happens is that Python doesn't "know" or want to define an uppercase conversion for every single unicode symbol. So it takes the easy route by evaluating s.swapcase() as "SS", so thus s.swapcase().swapcase() is "ss".

like image 98
ruohola Avatar answered Mar 05 '23 16:03

ruohola


In fact, there is a wide range of examples: it happens with some greek symbols, german symbols, armenian symbols, and other specific/special symbols.

To get them all:

find_dif = lambda s: s.swapcase().swapcase() != s

[chr(s) for s in range(100000) if find_dif(chr(s))]

and you get:

['µ', 'ß', 'İ', 'ı', 'ʼn', 'ſ', 'ǰ', 'ͅ', 'ΐ', 'ΰ', 'ς', 'ϐ', 'ϑ', 'ϕ', 'ϖ', 'ϰ', 'ϱ', 'ϴ', 'ϵ', 'և', 'ᲀ', 'ᲁ', 'ᲂ', 'ᲃ', 'ᲄ', 'ᲅ', 'ᲆ', 'ᲇ', 'ᲈ', 'ẖ', 'ẗ', 'ẘ', 'ẙ', 'ẚ', 'ẛ', 'ẞ', 'ὐ', 'ὒ', 'ὔ', 'ὖ', 'ᾀ', 'ᾁ', 'ᾂ', 'ᾃ', 'ᾄ', 'ᾅ', 'ᾆ', 'ᾇ', 'ᾐ', 'ᾑ', 'ᾒ', 'ᾓ', 'ᾔ', 'ᾕ', 'ᾖ', 'ᾗ', 'ᾠ', 'ᾡ', 'ᾢ', 'ᾣ', 'ᾤ', 'ᾥ', 'ᾦ', 'ᾧ', 'ᾲ', 'ᾳ', 'ᾴ', 'ᾶ', 'ᾷ', 'ι', 'ῂ', 'ῃ', 'ῄ', 'ῆ', 'ῇ', 'ῒ', 'ΐ', 'ῖ', 'ῗ', 'ῢ', 'ΰ', 'ῤ', 'ῦ', 'ῧ', 'ῲ', 'ῳ', 'ῴ', 'ῶ', 'ῷ', 'Ω', 'K', 'Å', 'ff', 'fi', 'fl', 'ffi', 'ffl', 'ſt', 'st', 'ﬓ', 'ﬔ', 'ﬕ', 'ﬖ', 'ﬗ']

Let's check them out:

s1 = 'µ'
s2 = s1.swapcase().swapcase()

s1 == s2

False

s1 = 'ß'
s2 = s1.swapcase().swapcase()

s1 == s2

False

s1 = 'ﬗ'
s2 = s1.swapcase().swapcase()

s1 == s2

False

like image 41
sentence Avatar answered Mar 05 '23 17:03

sentence