🕯️ Magic Note
This method creates a new string where the case of each alphabetic character is reversed. It does not modify the original string. Unicode aware, so it works correctly with accented characters and non-English alphabets that have case distinctions.
- Returns a new string, original remains unchanged
- Only affects letters with case distinctions
- Numbers, spaces, and punctuation stay the same
- Works with Unicode characters like “Ä” and “ä”
For converting to a specific case (all uppercase or all lowercase), use .upper() or .lower() instead. swapcase() is best when you want to flip the existing case pattern rather than force a specific one.
| Input String | .swapcase() Result |
|---|---|
| “dreamy” | “DREAMY” |
| “HELLO” | “hello” |
| “Python Magic” | “pYTHON mAGIC” |
| “HeLLo WoRLd” | “hEllO wOrlD” |
| “Code123” | “cODE123” |
| “café” | “CAFÉ” |
For most everyday English text, it works perfectly. Also remember that the result is a completely new string, so repeated calls s.swapcase().swapcase() should return to the original string in simple cases.
Python
# Basic case flipping
text = “dreamy”
flipped = text.swapcase()
print(flipped)
# Output: DREAMY
print(text)
# Output: dreamy (original unchanged)
Python
# Flipping back and forth
original = “Magical”
once = original.swapcase()
twice = once.swapcase()
print(f”Once: {once}”)
print(f”Twice: {twice}”)
# Output: Once: mAGICAL
# Output: Twice: Magical
Python
# Mixed case transformation
sentence = “The Quick Brown Fox”
upside_down = sentence.swapcase()
print(upside_down)
# Output: tHE qUICK bROWN fOX
- Expecting .swapcase() to modify the original string, it returns a new string and leaves the original unchanged
- Using .swapcase() when .upper() or .lower() would be more appropriate for standardization
- Assuming .swapcase() works on numbers or symbols, it leaves them unchanged
⚡ Whisper
The dream flips. What was quiet shouts. What was loud whispers. Letters dance between their forms. Nothing is lost. Everything transforms. The meaning remains, only the voice changes.