When does using swapcase twice not return an identical answer?

后端 未结 3 1200
被撕碎了的回忆
被撕碎了的回忆 2021-01-03 18:57

The Python docs for str.swapcase() say:

Note that it is not necessarily true that s.swapcase().swapcase() == s.

I\'

3条回答
  •  没有蜡笔的小新
    2021-01-03 19:22

    This is the case when multiple letters are lower cases of the same letter.

    For example, the micro character µ (U+00B5) and the mu character μ (U+03BC):

    >>> u'\xb5'.swapcase()
    u'\u039c'
    >>> u'\u03bc'.swapcase()
    u'\u039c'
    

    The two are different characters, but their uppercase counterparts are the same. This means that when str.swapcase() is applied, they return the same character. However, doing this again can't (and won't) return both letters.

    >>> u'\xb5'.swapcase().swapcase()
    u'\u03bc'
    

提交回复
热议问题