Understanding Swapcase in Python

Understanding Swapcase in Python

4 mins read264 Views Comment
Updated on Mar 29, 2023 10:21 IST

The str class method swapcase() in Python changes the cases of the alphabetic characters in a string, making uppercase letters lowercase and lowercase letters uppercase.

2023_03_Understanding-Break-Statement-in-C-27.jpg

In this article, we will discuss the swapcase in Python and understand its use with the help of examples. We will be covering the following sections:

What is swapcase in Python?

The swapcase() function is a built-in method in Python that can be used with the string data type. This function allows you to convert uppercase letters to lowercase and vice versa within a string. It returns a new string with the modified case of the alphabetic characters, leaving non-alphabetic characters unchanged.

The swapcase() function is useful when you need to change the case of a string for formatting or presentation purposes. It can also be used in text processing applications to manipulate strings in various ways.

Recommended online courses

Best-suited Python courses for you

Learn Python with these high-rated online courses

– / –
40 hours
– / –
5 days
– / –
3 days
β‚Ή3 K
3 weeks
– / –
4 days
– / –
20 hours
– / –
2 months
Free
6 weeks
– / –
3 months

Syntax of swapcase()

The syntax of swapcase() function in Python is given as:

 
string.swapcase()
Copy code

Here, string is a variable that contains the string on which you want to perform the case conversion. The swapcase() method is called on the string variable using the dot notation.

The function does not take any parameters other than the string on which it is called.

When the function is called, it returns a new string object with the modified case of the alphabetic characters in the original string. The original string remains unchanged.

Examples of Using the swapcase in Python

Example 1: Using swapcase() to toggle the case of a string

 
original_string = "PyThOn iS aWeSoMe!"
modified_string = original_string.swapcase()
print(modified_string)
Copy code

Output:

 
PyThOn iS aWeSoMe!
Copy code

In this example, we start with a string that contains uppercase and lowercase letters. We call the swapcase() method on the string to create a new string with the case of each letter toggled. The new string is then printed on the screen.

Example 2: Using swapcase() to convert only alphabetic characters

 
original_string = "P@y#h0n i5 Aw3s0m3!"
modified_string = ''.join([i.swapcase() if i.isalpha() else i for i in original_string])
print(modified_string)
Copy code

Output:

 
p@Y#H0N I5 aW3S0M3!
Copy code

In this example, we start with a string that contains non-alphabetic characters as well as uppercase and lowercase letters. We use a list comprehension to iterate over each character in the string and apply the swapcase() method only to alphabetic characters. Non-alphabetic characters are added to the new string as-is using the else clause. The new string is then printed on the screen.

Example 3: Using swapcase() to format a string for display

 
name = "john doe"
formatted_name = name.title().swapcase()
print(formatted_name)
Copy code

Output:

 
jOHn dOE
Copy code

In this example, we start with a lowercase string representing a name. We use the title() method to capitalize the first letter of each word in the name, and then use swapcase() to toggle the case of each letter in the name. This results in a string that is formatted with alternating capitalization, suitable for display purposes. The formatted string is then printed on the screen.

Example 4: Using swapcase() to encode and decode a string

 
message = "The secret code is 12345"
encoded_message = message.swapcase()
decoded_message = encoded_message.swapcase()
print("Original message: ", message)
print("Encoded message: ", encoded_message)
Copy code

Output:

 
Original message: The secret code is 12345
Encoded message: tHE SECRET CODE IS 12345
Decoded message: The secret code is 12345
Copy code

In this example, we start with a string that contains a secret message. We use the swapcase() method to create an encoded version of the message by toggling the case of each letter. We then use swapcase() again on the encoded message to decode it back to its original form. The original message, encoded message, and decoded message are all printed to the screen for comparison.

Example 5: Using swapcase() with regular expressions to replace characters

 
import re
original_string = "Hello, World!"
modified_string = re.sub(r'[aeiou]', lambda x: x.group(0).swapcase(), original_string)
print(modified_string)
Copy code

Output:

 
hEllO, wOrld!
Copy code

In this example, we start with a string that contains uppercase and lowercase letters as well as punctuation. We use the re.sub() function to replace all vowels (a, e, i, o, u) with the same letter but with the case toggled. This is accomplished using a lambda function with the swapcase() method called on the match object. The modified string is then printed on the screen.

Endnotes

In conclusion, swapcase() is a convenient and easy-to-use method that can help you perform case conversions on strings in Python.

Hope this article was helpful for you to understand how and why the swapcase() method is used in Python. If you wish to learn more about Python and practice Python programming, you can explore related articles here.

Explore programming courses

FAQs

What types of strings can be used with the swapcase() function?

The swapcase() function can be used with any string object in Python, including literals, variables, and string methods that return strings.

Does the swapcase() function modify the original string?

No, the swapcase() function returns a new string object with the modified case of the alphabetic characters in the original string. The original string remains unchanged.

How does the swapcase() function handle non-alphabetic characters?

The swapcase() function only modifies the case of alphabetic characters in a string. Non-alphabetic characters are included in the new string as-is.

Can the swapcase() function be used in combination with other string methods?

Yes, the swapcase() function can be used in combination with other string methods to modify or format a string as desired.

What is the difference between swapcase() and lower() or upper() functions?

The lower() function converts all alphabetic characters in a string to lowercase, while the upper() function converts all alphabetic characters to uppercase. The swapcase() function, on the other hand, toggles the case of each alphabetic character in the string.

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio