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 Program to Check If Two Strings are Anagram
Python List copy()
Python Program to Count the Number of Each Vowel
Python Program to Count the Number of Occurrence of a Character in String
Converting String to Stream of chars
Python while Loop
Python Closures
Python Program to Check If a String Is a Number (Float)
Python Decorators
Python String lstrip()
Python Program to Slice Lists
Python staticmethod()
Python Program to Get the Full Path of the Current Working Directory
Converting String to Stream of chars
Python Program to Find HCF or GCD
Python Program to Check Armstrong Number
How to get current date and time in Python?
Python frozenset()
Python bin()
Python Program to Check Prime Number
Python Program to Print the Fibonacci sequence
Python Program to Find the Size (Resolution) of a Image
Python List index()
Python String isspace()
Python timestamp to datetime and vice-versa
Python Program to Display Calendar
Python String expandtabs()
Python Set intersection_update()
Python Dictionary update()
Python Program to Convert Decimal to Binary Using Recursion
Python Multiple Inheritance
Deep Learning in Python - LazyProgrammer