Python Glossary Русский

all Python 3Byte sequencesfunction

bytearray.swapcase

bytearray — case

Swap the case of ASCII letters.

Syntax

ba.swapcase(...)

On bytes the operation is exactly reversible: apply it twice and you get the original data back, since only the A-Z/a-z pairs are touched. Strings give no such guarantee — 'ß'.swapcase() yields 'SS', which cannot be swapped back — so do not carry str habits over to bytearray unchecked. Like the other case methods, it returns a copy instead of mutating in place.

Examples

print(bytearray(b'AbC').swapcase())   # → bytearray(b'aBc')
print(bytearray(b'Hello World').swapcase())   # → bytearray(b'hELLO wORLD')
print(bytearray(b'abc123!').swapcase())   # → bytearray(b'ABC123!')
ba = bytearray(b'Xy')
print(ba.swapcase())   # → bytearray(b'xY')
print(ba)   # → bytearray(b'Xy')

See also

Open in the full glossarydocs.python.orgReport an inaccuracy