Python swapcase() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

swapcase() flips the case of every letter: capitals become small letters and small letters become capitals. "Hello World" becomes "hELLO wORLD". Characters without case are copied unchanged.

Like all string methods it returns a new string. It is rarely needed, but it is a neat fix for text that was typed with Caps Lock on by mistake, and a good way to see how case works.

text.swapcase() is a method of the built-in str type: call it on a string with dot notation. No import is needed, and it never changes the original string.

Syntax

Call it on any string. There are no arguments, but the parentheses are required.

python
result = text.swapcase()
CallWhat it does
"Hello World".swapcase()Returns 'hELLO wORLD'.
"PYTHON 3".swapcase()Returns 'python 3'. The digit has no case and is kept.
"Python".swapcase().swapcase() == "Python"Returns True. Swapping twice gives the original back for plain English letters.

To force one case use lower() or upper(). To capitalize each word use title().

Library declarations
python
str.swapcase() -> str
# returns: a new string; the original is unchanged
# raises: nothing (works for any string)

Parameters

swapcase() takes no arguments. It always works on the whole string.

Return Value

Return ValueDescription
strA new string in which every upper case letter is lower case and every lower case letter is upper case. The original string is not changed.

Python Examples

Example 1 — Flip the Case of a Sentence

Swap the case, then swap it back to see the original return.

python
greeting = "Hello World"
flipped = greeting.swapcase()

print(flipped)
print(flipped.swapcase())
print(greeting)

How It Works

  1. Every letter flips. H becomes h, and the lower case ello becomes ELLO.
  2. Flip again. Calling swapcase() on the flipped text brings back "Hello World".
  3. No edit in place. greeting was never changed, so the last line prints the original.

Example 2 — Repair Text Typed With Caps Lock On

Text typed with Caps Lock on has its case reversed, which swapcase() repairs in one call.

python
typed = "hELLO, mY nAME IS rIA."
fixed = typed.swapcase()

print(typed)
print(fixed)

How It Works

  1. The mistake. Every letter was typed in the opposite case, so the sentence looks odd.
  2. One call repairs it. swapcase() flips every letter back to what the writer meant.
  3. Only for fully reversed text. swapcase() flips every letter, including ones that were typed correctly, so use it only when the whole text was typed with Caps Lock on.

Example 3 — Count Capitals Before and After

Count the capital letters of a string and of its flipped copy.

python
text = "Python 3.12 Rocks"
flipped = text.swapcase()

print(flipped)
print(sum(1 for ch in text if ch.isupper()))
print(sum(1 for ch in flipped if ch.isupper()))

How It Works

  1. Flip first. The digits and the dot stay, while P and R become lower case and the other letters become capitals.
  2. Two capitals at the start. The original has only P and R in upper case, so the first count is 2.
  3. Nine after the flip. The nine letters that were lower case are now capitals, so the second count is 9.

Frequently Asked Questions

It returns a new string in which every upper case letter is lower case and every lower case letter is upper case. Other characters are unchanged.
No. Strings are immutable, so swapcase() returns a new string. Assign the result if you want to keep it.
For plain English letters, yes. For a few international characters, such as the German sharp s, it may not.
Mostly for repairing text typed with Caps Lock on, or for small puzzles. Everyday code uses lower() and upper() much more often.
They have no case, so swapcase() copies them to the new string unchanged.

Next: title()

Return a copy with the first letter of every word in upper case.

title() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

10 people found this page helpful