In this tutorial, we will look at how to swap the case of a Python string with the help of some examples.
What does it mean to swap the case of a string?

Swapping the case in a string refers to changing lowercase letters in the string to uppercase and uppercase letters in the string to lowercase. For example, if we swap the case for the string “Peter Parker”, we’ll get the string “pETER pARKER”.
How to swap the case of a String in Python?
You can use the built-in Python string function, swapcase()
to reverse the case of the letters in a string. It changes the case of uppercase letters to lowercase and of the lowercase letters to uppercase. The following is the syntax –
# swap the case of string s s.swapcase()
It returns a new string with the case of the letters reversed from the original string.
Highlighted programs for you
Flatiron School
Flatiron School
University of Maryland Global Campus
University of Maryland Global Campus
Creighton University
Creighton University
Examples
Let’s now look at some examples.
# create a string s = "Peter Parker" # swap the case print(s.swapcase())
Output:
pETER pARKER
We get the resulting string in the swapped case.
Let’s look at another example.
# create a string s = "dOnT tYpE lIKe tHIs" # swap the case print(s.swapcase())
Output:
DoNt TyPe LikE ThiS
You can see that the case of the characters in the resulting string is reversed.
Note that if you want to change the case of the entire string to uppercase, lowercase, or titlecase, use their respective built-in functions.
# create a string s = "maTT murdock" # uppercase print(s.upper()) # lowercase print(s.lower()) # titlecase print(s.title())
Output:
MATT MURDOCK matt murdock Matt Murdock
Here, we used the string built-in functions, upper()
to convert the string to uppercase, lower()
to convert the string to lowercase, and title()
to convert the string to titlecase.
FAQs
You can use the string built-in swapcase()
function to swap (or reverse) the case of letters in a Python string.
No. Strings in Python are immutable and thus cannot be modified after creation. The swapcase()
function returns a new string with the letters having the swapped case from the original string.
You might also be interested in –
- Swap Adjacent Characters in a Python String with this Method
- Easily Swap Keys and Values in a Python Dictionary with this method
- Swap Two Words in a String with these Methods in Python
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.