How Can We Help?
In this tutorial, we will explain how to use Python string swapcase() method with basic syntax by example for better understanding.
How to Swap the Case of String in Python? To swap the case of a string in Python, we use the swapcase() method. The swapcase method in Python inverts the case of all the characters of a string. It returns a copy of the original string, but the only case is swapped like uppercase into lowercase and lowercase into uppercase.
Syntax:
<string_value/string_variable>.swapcase()
Input Parameters:
- string_value / String variable : The string that we want to invert the case of it’s characters.
Swap whole string characters to uppercase
Example (1)
msg= 'welcome to python with oraask'
print('Swapcase: ',msg.swapcase())
Output of example (1)
Swapcase: WELCOME TO PYTHON WITH ORAASK
Swap whole string characters to lowercase
Example (2)
msg= 'WELCOME TO PYTHON WITH ORAASK'
print('Swapcase: ',msg.swapcase())
Output of example (2)
Swapcase: welcome to python with oraask
In the above example, the value of the string variable ‘msg’ is in lowercase, and we have converted it into lowercase by msg.swapcase().
Swap lowercase characters to uppercase and vise versa
Example (3)
msg= 'WeLcOME TO PYtHOn WitH ORaaSK'
print('Swapcase: ',msg.swapcase())
Output of example (3)
Swapcase: wElCome to pyThoN wITh orAAsk
In the above example, the value of the string variable ‘msg’ is in mix case, and we have inverted the lowercase characters to uppercase characters and vice versa by msg.swapcase().
In this tutorial, you have learned the string swapcase() method in Python. What is it? And how to inverts the case of all the characters in a string.
Hopefully, it was clear and concise.
If you have an inquiry or doubt, don’t hesitate to leave them in a comment. We are waiting for your valuable feedback.
Similar Python String Formatting Methods:
- title() : It returns the “title cased” version of string means all the words start with uppercase and the remaining are lowercase.
- capitalize() : It capitalizes the first character of the String.
- center() : It returns a space-padded string with the original string centered to a total of width columns.
- lower() : It converts all the characters of a string to lowercase.
- upper() : It converts all the characters of a string to uppercase.
- rstrip() : It removes particular character of a string from right side.
- lstrip() : It removes particular character of a string from left side.
- strip(chars) : It removes particular characters of a string from left and right side.
- ljust() : It returns an original string with a filled specific character or space at the left side of the string value to justify the string’s new length (width).
- rjust() : It returns an original string with a filled specific character or space at the right side of the string value to justify the string’s new length (width).
- zfill() : It returns an original string value with filled zero at the left side of the string value to justify the string’s new length (width).
Related Articles
Python String – Mastering By Practical Examples (Comprehensive Guide)
Python Tutorials List:
- Python tutorials list : access all python tutorials.