Swapcase in Python | The writing systems that distinguish between the upper and lowercase have two parallel sets of letters, with each letter in one set usually having an equivalent in the other set. Lowercase letters are the shorter, smaller versions of letters (like w), as opposed to the bigger, taller versions (like W), which are called uppercase letters.
Uppercase letters are also known as capital letters. Uppercase letters signal to the reader that something is important or significant. English alphabet uppercase letters: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z.
In writing, most letters are lowercase. Lowercase letters are all letters that do not begin a sentence or refer to a proper noun. English alphabet lowercase letters: a b c d e f g h i j k l m n o p q r s t u v w x y z.
Swapcase Function in Python
This python program using the built-in function to swapping strings. We will take a string while declaring the variables. Then, the swapcase() function converts all lowercase characters to uppercase characters and all uppercase characters to lowercase characters of the given string and returns it.
The syntax of swapcase() method is:
The swapcase() method doesn’t take any parameters.
Return value from String swapcase():
The swapcase() function return a string with all the cases changed.
# Python program to swapping characters in given string # take input string = input('Enter any string: ') # swapcase() function to changing case print('Swap Case:', string.swapcase())
Output for the different input values:-
Enter any string: KNOW program
Swap Case: know PROGRAM
Enter any string: SWapCsE in PYthon
Swap Case: swAPcSe IN pyTHON
Enter any string: PYthON3
Swap Case: pyTHon3
Note:- string.swapcase().swapcase() == string
Swapcase in Python without swapcase()
This python program also performs the same task but in a different way. In the above program, we used swapcase() function but in this program, we are using isupper(), islower(), isspace(), upper(), and lower() function to converts all lowercase characters to uppercase characters and all uppercase characters to lowercase characters of the given string.
# Python program to swapping characters in given string # take input string = input('Enter any string: ') swap_string ='' for ch in string: # checking lowercase characters and # convert in uppercase characters if (ch.isupper()) == True: swap_string+=(ch.lower()) # checking uppercase characters and # convert in lowercase characters elif (ch.islower()) == True: swap_string+=(ch.upper()) # checking whitespace and adding in new string elif (ch.isspace()) == True: swap_string+= ch # print string after swapping print('After Swapping:',swap_string)
Enter any string: SWAPcase
After Swapping: swapCASE
The isupper() function is used to check if the string contains any uppercase characters. Then, the lower() function converts all uppercase characters in a string into lowercase characters. Similarly, the islower() function is used to check if the string contains any lowercase characters. Then, the upper() function converts all lowercase characters in a string into uppercase characters. The isspace() function is used to check if the string contains whitespace characters.
Also See:- Python program to check vowel or consonant
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!