Python bytes.swapcase() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
bytes method

Definition

bytes.swapcase() returns a new bytes object in which every ASCII lowercase letter is turned into uppercase and every ASCII uppercase letter into lowercase. All other bytes are copied unchanged.

In ASCII, an uppercase letter and its lowercase partner differ by exactly 32, so swapping case is the same as flipping one bit of each letter byte. For ASCII data, calling swapcase() twice always gives back the original.

bytes.swapcase() is a method of the built-in bytes type: call it on a bytes object with dot notation, such as data.swapcase(...). No import is needed, and bytearray objects have the same method.

Syntax

No arguments; the swapped copy is returned.

python
result = data.swapcase()
CallWhat it does
b"Hello".swapcase()Returns b'hELLO'.
b"a1B2".swapcase()Returns b'A1b2'.
bytearray(b"Ok").swapcase()Returns bytearray(b'oK').

The round-trip guarantee holds for bytes because only ASCII letters change; the str version can break it for some Unicode letters.

Method signatures
python
bytes.swapcase()
# returns a new bytes object (bytearray.swapcase() returns a bytearray)

Parameters

swapcase() takes no parameters.

Return Value

Return ValueDescription
bytesA copy with the case of each ASCII letter inverted.

Python Examples

Example 1 — Swap Case in ASCII and UTF-8 Bytes

Swap a greeting, a mixed token, and the UTF-8 bytes of a French word that contains accented letters.

python
print(b"Hello World 42".swapcase())
print(b"aBc-XyZ".swapcase())
print(b"\xc3\x89t\xc3\xa9".swapcase())

How It Works

  1. Every letter flips. H becomes h and the lowercase letters become capitals; the space and digits stay.
  2. Symbols in between. The hyphen is left alone while letters on both sides swap.
  3. Only the t changes. The word starts and ends with two-byte accented letters, which are not ASCII, so only the middle t becomes T.

Example 2 — Swapping Twice Restores ASCII Data

Swap a token, swap it back, and check the case tests on the intermediate value.

python
token = b"AbCdEf123"

once = token.swapcase()
twice = once.swapcase()
print(once)
print(twice == token)
print(once.isupper(), once.islower())

How It Works

  1. First swap. Alternating case flips position by position into b'aBcDeF123'.
  2. Reversible. A second swap restores the exact original bytes.
  3. Mixed case. The result has both upper and lower letters, so isupper() and islower() are both False.

Example 3 — See the Byte Values Change by 32

List the integer values before and after swapping to see the fixed distance between ASCII cases, then try a bytearray.

python
data = b"Ok"

print(list(data), list(data.swapcase()))
print(data.swapcase()[0] - data[0])
print(bytearray(b"Ok").swapcase())

How It Works

  1. Codes as ints. O is 79 and k is 107; after swapping, o is 111 and K is 75.
  2. Fixed distance. Lowercase codes are always 32 higher than uppercase ones: 111 - 79 = 32.
  3. bytearray version. The mutable type returns a new bytearray with the cases swapped.

Frequently Asked Questions

It returns a copy where ASCII lowercase letters become uppercase and uppercase letters become lowercase.
Yes for bytes, because only ASCII letters change and each one maps back to itself after two swaps.
No. Bytes 128-255 are copied unchanged.
No. bytes are immutable, so a new object is returned.
ASCII places A-Z at 65-90 and a-z at 97-122, so each pair is exactly 32 apart.

Next: bytes.title()

Return a copy where each word starts with an uppercase ASCII letter.

title() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful