swap the case of letters in a python string

Swap the Case of a Python String with the swapcase() method

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?

swap the case of letters in a python 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.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 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

How to reverse the case of a Python string?

You can use the string built-in swapcase() function to swap (or reverse) the case of letters in a Python string.

Does the swapcase() function modify the string in place?

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 –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top