How to Reverse Case in Python?

Estimated read time 1 min read

To reverse the case of characters in a string in Python, you can use the swapcase() method of the string. Here’s an example:

sentence = "Hello World"
reversed_case = sentence.swapcase()
print(reversed_case)  # Output: hELLO wORLD

In this example, we have a string “Hello World” assigned to the variable sentence. To reverse the case of the characters, we use the swapcase() method, which returns a new string with the case of each character reversed. The reversed case string is then assigned to the variable reversed_case. Finally, we print the reversed case string using the print() function, resulting in the output “hELLO wORLD”.

The swapcase() method changes uppercase characters to lowercase and lowercase characters to uppercase while leaving non-alphabetic characters unchanged.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply