This is a tutorial of the Python String swapcase() method. Learn to convert lowercase into uppercase & uppercase into lowercase in a given string.
str.swapcase() method converts all of the uppercase alphabets into lowercase and all of the lowercase alphabets into uppercase in a given string and returns it as a new string.
Note. The original string on which this method is applied remains unchanged.
To convert a given string into either case, i.e. into lowercase or uppercase only, you can use the Python String methods
Given below is the syntax of the Python String
swappedString = givenString.swapcase()
This method does not take any arguments as it only checks the given string on which it is applied, so no additional data is required in the form of arguments.
swapcase() Return Value
It returns the string created from the given String such that in the returned string, the cases of the alphabets are swapped. In other words, in the returned string all of the alphabets that were originally in lowercase in the given string are now in uppercase and similarly, the alphabets that were originally in uppercase in the given string are now in lowercase.
Example: Swapping Case using Python String
The following example demonstrates the usage of the Python String
#UPPERCASE String welcome = "WELCOME TO WTMATTER.COM" print(welcome.swapcase()) #lowercase String welcome = "welcome to wtmatter.com" print(welcome.swapcase()) #AlTeRnAtE CaSe welcome = "WeLcOmE To wTmAtTeR.CoM" print(welcome.swapcase())
welcome to wtmatter.com WELCOME TO WTMATTER.COM wElCoMe tO WtMaTtEr.cOm
As you can observe in the output, the string in UPPERCASE is converted into lowercase and the string in lowercase is converted into uppercase. In the alternate case string also, the lowercase alphabets are converted into uppercase and the uppercase alphabets are converted into lowercase.
I hope you found this guide useful. If so, do share it with others who are willing to learn Python and other programming languages. If you have any questions related to this article, feel free to ask us in the comments section.
And do not forget to subscribe to WTMatter!