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?
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"
.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With