Table of Contents
The string swapcase() method converts all uppercase characters to lowercase and all lowercase characters to uppercase characters of the given string, and returns it.
The format of swapcase()
method is:
string.swapcase()
Note: Not necessarily, string.swapcase().swapcase() == string
1. String swapcase() Parameters()
swapcase()
method doesn’t take any parameters.
2. Return value from String swapcase()
swapcase()
method returns the string where all uppercase characters are converted to lowercase, and lowercase characters are converted to uppercase.
3. Example 1: Swap lowercase to uppercase and vice versa using swapcase()
# example string string = "THIS SHOULD ALL BE LOWERCASE." print(string.swapcase()) string = "this should all be uppercase." print(string.swapcase()) string = "ThIs ShOuLd Be MiXeD cAsEd." print(string.swapcase())
Output
this should all be lowercase. THIS SHOULD ALL BE UPPERCASE. tHiS sHoUlD bE mIxEd CaSeD.
Note: If you want to convert string to lowercase only, use lower(). Likewise, if you want to convert string to uppercase only, use upper().
Related posts:
Python 3 for Absolute Beginners - Tim Hall & J.P Stacey
Python Program to Swap Two Variables
Python type()
Python Data Analytics with Pandas, NumPy and Matplotlib - Fabio Nelli
Python del Statement
Python String capitalize()
Python Program to Print Colored Text to the Terminal
Python strptime()
Python for Loop
Python Objects and Classes
Python hex()
Python break and continue
Deep Learning with Python - A Hands-on Introduction - Nikhil Ketkar
Deep Learning with Python - Francois Cholletf
Python List reverse()
Python max()
Python Program to Find the Factors of a Number
Python Program to Iterate Over Dictionaries Using for Loop
Python compile()
Python Program to Extract Extension From the File Name
Python Machine Learning Eqution Reference - Sebastian Raschka
Python Sets
String Set Queries
Python Functions
Python setattr()
Python Program to Find the Factorial of a Number
Split a String in Java
Python Program to Differentiate Between del, remove, and pop on a List
Python Set difference()
Python Variables, Constants and Literals
Python String lstrip()
Python Program to Get the Last Element of the List