Python String swapcase() Method – Convert Uppercase to Lowercase and Vice Versa

Python String swapcase() Method

The swapcase() method in Python returns a new string where all the uppercase letters are converted to lowercase and all the lowercase letters are converted to uppercase.

This is useful when you want to flip the case of all characters in a string.

Syntax

string.swapcase()

Parameters:

  • This method does not take any parameters.

Returns:

  • A new string with uppercase and lowercase letters swapped.

Example 1: Basic Usage

text = "Hello World"
print(text.swapcase())
hELLO wORLD

Example 2: With Mixed Case and Numbers

text = "Python3 Is Fun!"
print(text.swapcase())
pYTHON3 iS fUN!

Use Cases

  • Toggle text case for formatting or UI effects
  • Text normalization when case inversion is needed
  • Creating puzzles or challenges based on text transformations

Important Notes

  • The original string remains unchanged. swapcase() returns a new string.
  • Non-alphabetic characters (digits, symbols, spaces) are not affected.

Interview Tip

swapcase() can be useful in string manipulation problems, especially in interviews that test attention to detail in case handling.

Summary

  • swapcase() swaps lowercase letters to uppercase and vice versa
  • It returns a new string; the original string is unchanged
  • No arguments are needed to use this method

Practice Problem

Write a program that takes a sentence from the user and prints the case-swapped version.

sentence = input("Enter a sentence: ")
print("Swapcase version:", sentence.swapcase())