Python .swapcase()

design2461360801's avatar
Published Jun 10, 2021Updated Oct 13, 2023
Contribute to Docs

Takes a string and returns a copy of that string in which all lowercase letters are uppercase, and all uppercase letters are lowercase. Numbers and symbols are not changed.

  • Learn to analyze and visualize data using Python and statistics.
    • Includes 8 Courses
    • With Certificate
    • Intermediate.
      13 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours

Syntax

string.swapcase()

Example 1

message = "Hello, World!"
new_string = message.swapcase()
print(new_string)
# Output: "hELLO, wORLD!"

Example 2

The .swapcase() method does not change the string it is used on:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn Python on Codecademy

  • Learn to analyze and visualize data using Python and statistics.
    • Includes 8 Courses
    • With Certificate
    • Intermediate.
      13 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours