Uppercase and lowercase writing systems have two parallel sets of letters, with each letter in one set usually having an equivalent in the other. Lowercase letters are the shorter, smaller versions of letters (such as a), as opposed to uppercase letters, which are larger, taller counterparts (such as A). Python swapcase() is a highly useful technique in Python for changing the case of letters in a string. Python swapcase() returns a copy of the string in which all case-sensitive characters have had their cases swapped. Let us understand more about the swapcase() function concept along with programming examples.
Definition
- Python swapcase() string method returns a copy of the string by converting all the uppercase characters to lowercase and all lowercase characters to uppercase of the given input string.
- The Python swapcase() is a built-in string method that is used to return a string by converting all the uppercase characters to lowercase and vice versa.
Python swapcase() Syntax
The Python swapcase() function follows the below-mentioned syntax:
string.swapcase()
swapcase() Parameters
Python swapcase() does not accept any parameters. If any parameter is passed to the function, the Python interpreter will throw a TypeError exception.
Return Value from swapcase()
The swapcase() method returns a string with all uppercase characters changed to lowercase and all lowercase characters transformed to uppercase.
Example 1: Swap lowercase to uppercase and vice versa using swapcase()
Example
# Python program to illustrate swapcase()
txt = 'convert to uppercase'
print('Original string:', txt)
print('Case changed:', txt.swapcase())
print('')
txt = 'CONVERT TO LOWERCASE'
print('Original string:', txt)
print('Case changed:', txt.swapcase())
print('')
txt = 'MiXeD CaSe StRiNg'
print('Original string:', txt)
print('Case changed:', txt.swapcase())
Output
Original string: convert to uppercase
Case changed: CONVERT TO UPPERCASE
Original string: CONVERT TO LOWERCASE
Case changed: convert to lowercase
Original string: MiXeD CaSe StRiNg
Case changed: mIxEd cAsE sTrInG
Example 2: swapcase() with other Character Types
If the input string contains characters of different types such as numbers, signs, symbols, etc. Python swapcase() will ignore such characters and convert the alphabetic characters to their respective opposite cases.
Example
# Python program to illustrate swapcase()
txt = '2468 hello WORLD'
print('Original string:', txt)
print('Case changed:', txt.swapcase())
print('')
txt = '@$%* Python'
print('Original string:', txt)
print('Case changed:', txt.swapcase())
Output
Original string: 2468 hello WORLD
Case changed: 2468 HELLO world
Original string: @$%* Python
Case changed: @$%* pYTHON
Alternative methods to Python swapcase()
Python contains a wide range of string methods, and few of them can perform tasks similar (not exactly the same) to the swapcase() function. We utilized the swapcase() function in the previous program, but in the below program, with the help of Python isupper(), islower(), isspace(), upper(), and lower() functions we will change all the lowercase characters to uppercase characters and all the uppercase characters to lowercase characters of the input string.
Example
# Python program to swap characters in a given string
string = input('Enter any string: ')
swap_string = ''
for i in string:
   # checking lowercase characters and
   # converting to uppercase
   if(i.isupper()) == True:
       swap_string += (i.lower())
   # checking uppercase characters and
   # converting to lowercase
   elif (i.islower()) == True:
       swap_string += (i.upper())
   # checking whitespace and adding in new string
   elif (i.isspace()) == True:
       swap_string += i
# print string after swapping
print('After Swapping:',swap_string)
Output
Enter any string: Good MorniNG EvERyOnE
After Swapping: gOOD mORNIng eVerYoNe
Explanation
The isupper() method determines whether a string contains any uppercase characters or not. The lower() function is then used to transform all capital characters in a string to lowercase characters. Similarly, the islower() function determines whether the string contains any lowercase characters or not. The upper() function is then used to transform all lowercase characters in a string to uppercase characters. The isspace() method determines whether a string contains whitespace characters or not.
Frequently Asked Questions
Q1. How do you lowercase all letters in Python?
To lowercase all the characters in the input string, we use the Python lower() function. Python lower() converts all capital characters in a string to lowercase characters and returns the result. The lower() method returns a duplicate of an original string with all characters in lowercase. The syntax is as below:
string.lower()
Example
txt = 'WE ARE GOING TO PARIS FOR OUR VACATION'
print(txt.lower())
txt = 'wE ENjoY RainY SeasoN'
print(txt.lower())
Output
we are going to paris for our vacation
we enjoy rainy season
Q2. How do you change all the lowercase characters to uppercase in Python?
The upper() method converts all case-sensitive characters in a string to uppercase. Python upper() function is an inbuilt string class method that converts all the lowercase characters in the string into uppercase characters and returns a new string. The upper() function also ignores any numerals, special characters, or uppercase letters in the supplied string.
Example
# Python program to illustrate upper()
txt = 'Good Morning, Everyone'
print(txt.upper())
txt = 'welcome home jimmy'
print(txt.upper())
txt = 'Mobile No - 9341239145'
print(txt.upper())
txt = 'HELLO'
print(txt.upper())
Output
GOOD MORNING, EVERYONE
WELCOME HOME JIMMY
MOBILE NO - 9341239145
HELLO
Q3. What are islower and isupper in Python?
- Python islower() – This function returns True if all of the alphabets in the string are lowercase and if the string contains at least one capital alphabet, it returns False.
- Python isupper() – This function returns True if all the values in the input string are in uppercase, else it returns False.
Example
txt = 'GOOD MORNING EVERYONE'
print('Is text lowercased? - ', txt.islower())
print('Is text uppercased? - ', txt.isupper())
txt = '101 python program'
print('Is text lowercased? - ', txt.islower())
print('Is text uppercased? - ', txt.isupper())
Output
Is text lowercased? -Â False
Is text uppercased? -Â True
Is text lowercased? -Â True
Is text uppercased? -Â False
Leave a Reply