In Python, the swapcase()
method is a built-in string method that allows you to convert the case of characters in a string. It swaps uppercase characters to lowercase and lowercase characters to uppercase.
Here's the syntax of the swapcase()
method:
string.swapcase()
swapcase()
method returns a new string with the case-swapped characters, leaving the original string unchanged.Here are some examples to illustrate how the swapcase()
method works:
# Example 1
text = "Hello, World!"
swapped_text = text.swapcase()
print(swapped_text) # Output: hELLO, wORLD!
# Example 2
sentence = "ThiS Is a MixEd cAsE StrIng."
new_sentence = sentence.swapcase()
print(new_sentence) # Output: tHIs iS A mIxEd CaSe sTRiNG.
In the above examples, the swapcase()
method converts uppercase characters to lowercase and lowercase characters to uppercase, effectively swapping their cases in the resulting string.