Python 3 – String swapcase() Method
One of the most interesting things about Python programming language is the range of built-in functions it offers. These functions are incredibly useful when it comes to performing tasks such as string manipulation, number crunching, and data handling. One such function is the swapcase()
method.
The swapcase()
method is a built-in function in Python 3 that can be used to swap the case of characters in a string. In other words, it changes uppercase letters to lowercase and vice versa. This method is easy to use and can come in handy when we need to manipulate strings for certain purposes.
Here’s how the swapcase()
method works:
Syntax
str.swapcase()
The swapcase()
method is called on a string object and it does not take any arguments.
Parameters
The swapcase()
method does not accept any parameters.
Return value
The swapcase()
method returns a new string with the case of all characters in the original string swapped.
Example code
string = "hElLo wOrLd"
new_string = string.swapcase()
print(new_string)
The output of the above code will be:
HeLlO WoRlD
Here, we first define a string named ‘string’. We then call the swapcase()
method on this string using ‘string.swapcase()’, which returns a new string with all the uppercase letters swapped with lowercase letters and vice versa. We then assign this new string to a variable named ‘new_string’. Finally, we print the value of the new string.
Trying it Out
Let us now explore various examples where we can use the swapcase()
method for different purposes.
Example 1: Converting all lowercase letters to uppercase
string = "hello world"
new_string = string.swapcase()
print(new_string)
Output:
HELLO WORLD
In this code snippet, we start with a string that has all lowercase letters. We then use the swapcase()
method to convert all the letters in the string to uppercase.
Example 2: Converting all uppercase letters to lowercase
string = "HELLO WORLD"
new_string = string.swapcase()
print(new_string)
Output:
hello world
In this code snippet, we start with a string that has all uppercase letters. We then use the swapcase()
method to convert all the letters in the string to lowercase.
Example 3: Swapping the case of a mixed case string
string = "Hello World"
new_string = string.swapcase()
print(new_string)
Output:
hELLO wORLD
In this code snippet, we start with a string that has mixed case letters. We then use the swapcase()
method to swap the case of all the letters in the string.
Example 4: Applying swapcase()
method to a string with numbers and special characters
string = "H3ll0 W0rld!@#$%"
new_string = string.swapcase()
print(new_string)
Output:
h3LL0 w0RLD!@#$%
In this code snippet, we start with a string that has numbers and special characters along with alphabets. We then use the swapcase()
method to swap the case of all the alphabetic characters.
Conclusion
The swapcase()
method is a useful built-in function in Python that can help us swap the case of characters in a string with ease. It is simple to use and does not take any parameters. We can use this method in different ways to achieve our requirements. It is ideal for manipulating text and making it more suitable for certain types of analysis. Overall, swapcase()
method is an excellent addition to Python’s already impressive list of built-in functions.