Saturday, January 8, 2022

ItsMyCode: Python String swapcase()

ItsMyCode |

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.

The post Python String swapcase() appeared first on ItsMyCode.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...