In this tutorial, we will explain how to use Swapcase in Python with basic syntax by example for better understanding.
What is swapcase() in Python? The swapcase() method is a built-in Python function designed for use with strings. It transforms all uppercase letters in the string to lowercase, and all lowercase letters to uppercase. The function returns a new string reflecting these changes, while leaving any non-alphabetic characters untouched.
Syntax of Swapcase in Python Method:
<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)