English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python Basic Tutorial

Python Flow Control

Python Functions

Python Data Types

Python File Operation

Python Object and Class

Python Date and Time

Advanced Knowledge of Python

Python Reference Manual

Python string swapcase() usage and example

Python string methods

The swapcase() method of the string converts all uppercase characters in the given string to lowercase and all lowercase characters to uppercase, and then returns the converted string.

The format of the swapcase() method is:

string.swapcase()

Note:It may not be string.swapcase().swapcase() == string

String swapcase() parameters

The swapcase() method does not take any parameters.

String swapcase() return value

The swapcase() method returns a string in which all uppercase characters are converted to lowercase, and all lowercase characters are converted to uppercase.

Example1: Use swapcase() to switch lowercase to uppercase and vice versa

# String example
string = "THIS SHOULD ALL BE LOWERCASE."
print(string.swapcase())
string = "this should all be uppercase."
print(string.swapcase())
string = "ThIs ShOuLd Be MiXeD cAsEd."
print(string.swapcase())

When running the program, the output is:

this should all be lowercase.
THIS SHOULD ALL BE UPPERCASE.
tHiS sHoUlD bE mIxEd CaSeD.

Note:If you only want to convert a string to lowercase, please uselower(). Similarly, if you want to convert a string to uppercase only, please useupper().

Python string methods