swapcase() in Python

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

The swapcase() method of str class converts uppercase alphabetic characters of a string to lowercase and lowercase alphabetic characters of the same string to uppercase.

Syntax for swapcase() in Python

The syntax of swapcase function in Python is:

where string is the name of the string on which swapcase is performed.

Parameters for swapcase() in Python

The swapcase method in Python doesn't take any parameters.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Return Values for swapcase() in Python

Return Type: str

swapcase() in Python returns a string where all uppercase alphabetic characters are converted to lowercase, and lowercase alphabetic characters are converted to uppercase. Special characters and numbers remain unchanged.

Example of swapcase() in Python

Output

What is swapcase() Function in Python?

Python's str class provides several methods to perform some common operations on a string.

One of such method is swapcase(). Swapcase method is used for converting uppercase letters of a string to lowercase and lowercase letters of a string to uppercase.

Note:

  • The swapcase function does not modify the original string, it only returns a new string created by swapping the cases.
  • Numbers and special characters are not modified.
  • It is not necessarily true that s.swapcase().swapcase() == s.
    • There are some unicode lower case letters such as the micro character µ (U+00B5) and the mu character μ (U+03BC) which have the same uppercase couterparts that is u'\u039c'. In that case doing u'\xb5'.swapcase().swapcase() will not be true. Because u'\xb5 will be changed to u'\u039c' on first application of swapcase and then u'\u039c' will be changed to u'\u03bc' on second application of swapcase.
    • Another example of such case is with the German consonant ß. It becomes SS after one application of swapcase and then ss after the second. See "More Examples" section for code and details.

More Examples

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Example 1: Swap lowercase to uppercase and vice versa using swapcase()

Output:

Explanation:

This is the usual behaviour of swapcase function in Python as discussed before, it will transform lowercase to uppercase and vice versa.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Example 2: Using swapcase() with a string containing special characters

Output:

Explanation:

The swapcase method does not affect any number or special character when transforming a string. In the example, we have a string that contains $12345@6789#, this remains unchanged after the applying swapcase() function and rest of the letters are modified.

Example 3: Not necessarily, s.swapcase().swapcase() == s

Output:

Explanation:

Why ss? Because the German consonant ß has been assigned SS as its uppercase counterpart. Thus, 'ß'.swapcase() returns SS which is then converted to ss on second application of swapcase.

Example 4: Real-world Use of swapcase()

Output:

Explanation:

Suppose you are creating a tool that helps people who forgot to turn off their capslock before writing. That's why they wrote uppercase letters by default and lowercase letters when they pressed Ctrl + shift + key. Now they need to fix the text by converting lowercase to uppercase and vice-versa.
In this example, We took the text as input and used swapcase to return the desired text.

Conclusion

  • The swapcase() method of str class converts uppercase characters to lowercase and lowercase characters to uppercase.
  • Swapcase function does not modify the original string, it returns a new string.
  • Swapcase function does not affect numbers and special characters of a string.

See Also:

Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more