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:
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj
Python Artificial Intelligence Project for Beginners - Joshua Eckroth
Python Program to Get the Class Name of an Instance
Python Program Read a File Line by Line Into a List
Python Program to Remove Punctuations From a String
Python print()
Python String title()
APIs in Node.js vs Python - A Comparison
Python Program to Find Factorial of Number Using Recursion
Python bytearray()
Python Set issuperset()
Python Set copy()
Python Set isdisjoint()
Python Program to Get the File Name From the File Path
Python Set intersection()
Python vars()
String Processing with Apache Commons Lang 3
Python frozenset()
Python Program to Sort a Dictionary by Value
Python String endswith()
Python set()
Python Program to Count the Occurrence of an Item in a List
Python compile()
Python Machine Learning Second Edition - Sebastian Raschka & Vahid Mirjalili
Python Dictionary items()
Deep Learning in Python - LazyProgrammer
Python str()
Python Program to Get Line Count of a File
Convert String to int or Integer in Java
Python Type Conversion and Type Casting
Python len()
Python Program to Count the Number of Each Vowel