In the previous article, we have discussed Program for Python String title() Function
Python String swapcase() Function:
The swapcase() method returns a string in which all upper case letters are converted to lower case and vice versa.
Syntax:
string.swapcase()
Parameters: This function doesn’t have any parameters.
Examples:
Example1:
Input:
Given first string = "welcome to PYTHON-Programs" Given second string = "HELLO ALL GOOD MORNING"
Output:
The above given first string is : welcome to PYTHON-Programs The given first string after applying swapcase() function: WELCOME TO python-pROGRAMS The above given second string is : HELLO ALL GOOD MORNING The given second string after applying swapcase() function: hello all good morning
Example2:
Input:
Given first string = "hello ALL gOOD MORNing" Given second string = "HELLO this is BTECHGEEKS"
Output:
The above given first string is : hello ALL gOOD MORNing The given first string after applying swapcase() function: HELLO all Good mornING The above given second string is : HELLO this is BTECHGEEKS The given second string after applying swapcase() function: hello THIS IS btechgeeks
Program for String swapcase() Function in Python
Method #1: Using Built-in Functions (Static Input)
Approach:
- Give the first string as static input and store it in a variable.
- Apply swapcase() function to the given first string that returns a string in which all upper case letters are converted to lower case and vice versa.
- Store it in another variable.
- Print the above-given String.
- Print the above-given String after applying the swapcase() function.
- Similarly, do the same for the other string and print the result string.
- The Exit of Program.
Below is the implementation:
# Give the first string as static input and store it in a variable. gvn_fststr = "welcome to PYTHON-Programs" # Apply swapcase() function to the given first string that returns a string # in which all upper case letters are converted to lower case and vice versa. # Store it in another variable. rslt_str1 = gvn_fststr.swapcase() # Print the above given String print("The above given first string is :", gvn_fststr) # Print the above given String after applying swapcase() function. print("The given first string after applying swapcase() function:") print(rslt_str1) # Similarly do the same for other string and print the result string. gvn_scndstr = "HELLO ALL GOOD MORNING" rslt_str2 = gvn_scndstr.swapcase() print("The above given second string is :", gvn_scndstr) print("The given second string after applying swapcase() function:") print(rslt_str2)
Output:
The above given first string is : welcome to PYTHON-Programs The given first string after applying swapcase() function: WELCOME TO python-pROGRAMS The above given second string is : HELLO ALL GOOD MORNING The given second string after applying swapcase() function: hello all good morning
Method #2: Using Built-in Functions (User Input)
Approach:
- Give the first string as user input using the input() function and store it in a variable.
- Apply swapcase() function to the given first string that returns a string in which all upper case letters are converted to lower case and vice versa.
- Store it in another variable.
- Print the above-given String.
- Print the above-given String after applying the swapcase() function.
- Similarly, do the same for the other string and print the result string.
- The Exit of Program.
Below is the implementation:
# Give the first string as user input using the input() function and store it in a variable. gvn_fststr = input("Enter some Random String = ") # Apply swapcase() function to the given first string that returns a string # in which all upper case letters are converted to lower case and vice versa. # Store it in another variable. rslt_str1 = gvn_fststr.swapcase() # Print the above given String print("The above given first string is :", gvn_fststr) # Print the above given String after applying swapcase() function. print("The given first string after applying swapcase() function:") print(rslt_str1) # Similarly do the same for other string and print the result string. gvn_scndstr = input("Enter some Random String = ") rslt_str2 = gvn_scndstr.swapcase() print("The above given second string is :", gvn_scndstr) print("The given second string after applying swapcase() function:") print(rslt_str2)
Output:
Enter some Random String = hello ALL gOOD MORNing The above given first string is : hello ALL gOOD MORNing The given first string after applying swapcase() function: HELLO all Good mornING Enter some Random String = HELLO this is BTECHGEEKS The above given second string is : HELLO this is BTECHGEEKS The given second string after applying swapcase() function: hello THIS IS btechgeeks
Are you facing difficulties in finding all the methods that a string object can call in python? Have a glance at this Python String Method Examples Tutorial & meet such challenges with ease.