swapcase(): Upper case chars to Lower case and and vice versa
All upper case chars are changed to lower case and lower case chars are changed to upper case.
Basic usage:
Inverts the case of each character, making uppercase letters lowercase and vice versa.
text = "Hello World"
result = text.swapcase()
print(result) # Output: "hELLO wORLD"
my_str='Welcome to plus2net Python Section'
output=my_str.swapcase()
print(output)
Output is here
wELCOME TO PLUS2NET pYTHON sECTION
Working with numbers and special characters:
Non-alphabetic characters like numbers and symbols remain unchanged.
text = "Python3.9 is AWESOME!"
result = text.swapcase()
print(result) # Output: "pYTHON3.9 IS awesome!"
Handling mixed case strings:
text = "WeLcOmE tO PyThOn"
result = text.swapcase()
print(result) # Output: "wElCoMe To pYtHoN"
Python String swapcase():
Example 2: Using swapcase() with Mixed Cases
text = "Python3.8 is AWESOME"
result = text.swapcase()
print(result)
Output
pYTHON3.8 IS awesome
Example 3: swapcase() in List Comprehensions
Convert each string in a list to swapcased format.
text_list = ["Hello", "World", "Python"]
swapped = [text.swapcase() for text in text_list]
print(swapped)