str.swapcase()

str.swapcase()
Returns: str · Added in v3.x · Updated March 13, 2026 · String Methods
string method case conversion

The .swapcase() method returns a copy of the string with uppercase characters converted to lowercase and lowercase characters converted to uppercase. This is useful for inverting case, creating stylized text, and normalizing string data with mixed casing.

Syntax

str.swapcase()

Parameters

This method takes no parameters.

Return Value

Returns a new string with each character’s case swapped. Non-letter characters remain unchanged.

Examples

Basic Usage

>>> "Hello World".swapcase()
'hELLO wORLD'

>>> "Python Programming".swapcase()
'pYTHON pROGRAMMING'

>>> "UPPERCASE".swapcase()
'uppercase'

Mixed Case Strings

>>> "PyThOn".swapcase()
'pYtHoN'

>>> "AlReady MiXeD".swapcase()
'aLrEADY mIxEd'

Numbers and Symbols

>>> "Hello123!".swapcase()
'hELLO123!'

>>> "abc123XYZ".swapcase()
'ABC123xyz'

Numbers, punctuation, and whitespace remain unchanged—only alphabetic characters are affected.

Common Use Cases

Text Styling and Display

# Create inverted case for visual emphasis
title = "Featured Article"
print(title.swapcase())
# FEATURED aRTICLE

# Generate alternating case (manual approach)
def alternating_case(text):
    result = ""
    for i, char in enumerate(text):
        result += char.swapcase() if i % 2 == 0 else char
    return result

print(alternating_case("hello"))
# HeLlO

Data Normalization

# Handle mixed-case user input
user_input = "JaVaScRiPt"
normalized = user_input.swapcase()
print(normalized)
# jAvAsCrIpT

# Toggle case for password generation hints
base_word = "Secret"
hint = base_word.swapcase()
print(f"Hint: the word is {hint}")
# Hint: the word is sECRET

Processing String Collections

words = ["Python", "JAVA", "GoLang"]

# Convert all to swapped case
swapped = [w.swapcase() for w in words]
print(swapped)
# ['pYTHON', 'java', 'gOLANG']

# Filter by case pattern
mixed_case = [w for w in words if w != w.upper() and w != w.lower()]
print(mixed_case)
# ['Python', 'GoLang']

Behavior Details

How It Works

The method iterates through each character and applies these rules:

  • If a character is uppercase (A-Z), it becomes lowercase (a-z)
  • If a character is lowercase (a-z), it becomes uppercase (A-Z)
  • All other characters pass through unchanged
>>> "@#Hello%^".swapcase()
'@#hELLO%^'

>>> "abc DEF 123".swapcase()
'ABC def 123'

Multiple Swaps Return Original

text = "Hello"

# Swapping twice returns the original
print(text.swapcase().swapcase())
# Hello

# This is useful for case-rotation logic
def rotate_case(text):
    return text.swapcase()

Unicode Support

# Greek letters
>>> "ΣΠ".swapcase()
'σπ'

# Cyrillic
>>> "Питон".swapcase()
'питон'

# Works with accented characters
>>> "École".swapcase()
'éCOLE'

Differences From Similar Methods

MethodBehavior
.swapcase()Upper → lower, lower → upper
.lower()All → lowercase
.upper()All → uppercase
.title()Each word’s first char → uppercase
.capitalize()First char → uppercase, rest → lowercase
.casefold()Aggressive lowercase for comparison
>>> "Hello World".lower()
'hello world'

>>> "Hello World".upper()
'HELLO WORLD'

>>> "Hello World".swapcase()
'hELLO wORLD'

Edge Cases

Empty String

>>> "".swapcase()
''

All Same Case

>>> "ALLUPPERCASE".swapcase()
'alllowercase'

>>> "alllowercase".swapcase()
'ALLLOWERCASE'

Whitespace Handling

>>> "  Hello  ".swapcase()
'  hELLO  '

# Only the letters are swapped, whitespace remains

Performance

.swapcase() creates a new string and runs in O(n) time where n is the string length. The method is implemented in C and is efficient for typical use cases. Memory usage scales with the input string size.

See Also