swapcase() : Toggle Case of Each Character

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)
Output
['hELLO', 'wORLD', 'pYTHON']

All String methods


Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer