bytes.swapcase()

Rarely what you need, but occasionally exactly right. On ASCII it is a true involution — apply it twice and you are back where you started.

Bytes methodPython 3.0+Live demo
Common call
data.swapcase()
Returns
a new bytes object; the original is unchanged
Replaces
separate upper and lower passes with a per-byte test
Watch out
only ASCII letters flip; accented letters pass through
bytes.swapcase()
→ bytes

Demo

Live evaluation
Try:
Inputs
sstrdata (encoded as utf-8)
Output
bytes('HeLLo', 'utf-8').swapcase()
b'hEllO'

Every ASCII letter goes to the opposite case in one pass, so HeLLo becomes hEllO. All-lowercase becomes all-uppercase and vice versa. Digits and other non-letters are untouched. Applying it a second time restores the original exactly, which is not true of the str version once non-ASCII case mappings get involved.

Common patterns

Toggle a case-flag convention
Some legacy formats encode a flag as letter case.
toggled = field.swapcase()
Round-trip test for case handling
A cheap sanity check that a transform preserves case information.
assert data.swapcase().swapcase() == data
Swap case in real text
Decode first so non-ASCII letters take part.
data.decode('utf-8').swapcase().encode('utf-8')

Examples

1. Mixed
b'HeLLo'.swapcase()
Returns
b'hEllO'
2. Lowercase
b'hello'.swapcase()
Returns
b'HELLO'
3. Uppercase
b'HELLO'.swapcase()
Returns
b'hello'
4. Digits stay
b'aB1cD2'.swapcase()
Returns
b'Ab1Cd2'
5. Twice is identity
b'HeLLo'.swapcase().swapcase()
Returns
b'HeLLo'
6. Empty
b''.swapcase()
Returns
b''

Pitfalls

1. Non-ASCII letters do not flip
Same limit as upper and lower — only 52 byte values are recognised. Accented letters keep their case, so the result is inconsistently flipped.
Accent ignored
'hÉllo'.encode().swapcase()
b'H\xc3\x89LLO'
Work on text
'hÉllo'.swapcase().encode()
b'H\xc3\xa9LLO'
2. It returns a new object
Bytes are immutable. The result must be assigned or it is lost.
Result dropped
data.swapcase()
data
unchanged
Assign it
data = data.swapcase()
flipped
3. Not a case-insensitive comparison tool
Flipping case does not normalise it. To compare regardless of case, lower both sides — swapcase just produces a different mixed-case value.
Wrong tool
b'ABC'.swapcase() == b'abc'.swapcase()
False
Normalise instead
b'ABC'.lower() == b'abc'.lower()
True

When to use

Use it
  • Legacy formats that encode a flag in letter case
  • Round-trip tests of case handling
  • Deliberate case inversion of ASCII text
Reach for something else
  • Case-insensitive comparison → lower both sides
  • Real text with non-ASCII letters → decode first
  • Binary data, where A-Z and a-z bytes would be corrupted

Notes

Complexity
O(n) — one pass over the bytes
Return
A new bytes object of the same length
CPython impl
Objects/bytesobject.c :: stringlib_swapcase
Memory
Allocates a buffer the same size as the input
Thread-safe
Yes — bytes are immutable

FAQ

Rarely — mostly for legacy formats that encode a flag as letter case, and as a round-trip test. It exists on bytes mainly for parity with str.

History

3.0
bytes.swapcase arrived with the bytes type in the text/binary split.