Python Glossary Русский

all Python 3Byte sequencesfunction

bytes.swapcase

bytes — case

Swap the case of ASCII letters.

Syntax

b.swapcase(...)

Only ASCII letters flip; every other byte, including the bytes of UTF-8-encoded non-Latin letters, passes through untouched. Unlike str.swapcase, applying it twice to bytes always restores the original — for strings that is not guaranteed, since some characters expand when upper-cased.

Examples

print(b'AbC'.swapcase())   # → b'aBc'
print(b'Hello World'.swapcase())   # → b'hELLO wORLD'
print(b'Py3.12!'.swapcase())   # → b'pY3.12!'
print(b'AbC'.swapcase().swapcase())   # → b'AbC'
print('привет'.encode().swapcase() == 'привет'.encode())   # → True

See also

Open in the full glossarydocs.python.orgReport an inaccuracy