Python String swapcase()

Python string swapcase() method is a built-in function that converts all uppercase characters into lowercase and all lowercase characters into uppercase characters of a given string and returns a new string.

swapcase() Syntax

The Syntax of swapcase() method is:

string.swapcase()

swapcase() Parameters

The swapcase() method doesn’t take any parameters.

swapcase() Return Value

The swapcase() method returns a copy of the string where all the uppercase characters are converted into lowercase characters and vice-versa.

In case the string has other than alphabets those characters will not be converted and returned as-is.

Example: Python Program to change the case of a given string

text1 = "pYTHON tUTORIALS"
print(text1.swapcase())

text2 = "HELLO WORLD"
print(text2.swapcase())

text3 = "welcome to itsmycode"
print(text3.swapcase())

text4 ="12345!!!"
print(text4.swapcase())

Output

Python Tutorials
hello world
WELCOME TO ITSMYCODE
12345!!!
Note: If you want to convert given string into lowercase only it is recommended to use lower() method. Likewise, if you want to convert given string into uppercase only it is recommended to use upper() method.
Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like
Sort Dictionary By Value In Python

Sort Dictionary by value in Python

Table of Contents Hide Python sorted() function to rescueSyntax: sorted(iterable, key, reverse)Sort a Dictionary by ValueExample 1: Basic Sorting in PythonExample 2 : Sorting different Data Types in PythonExample 3: Sort…
View Post