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.
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.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
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.