In the previous article, we have discussed Python Program for capitalize() Function
casefold() Function in Python:
The casefold() method produces a string in which all of the characters are lower case.
This method is similar to lower(), but the casefold() method is stronger and more aggressive, which means it will convert more characters into lower case and find more matches when comparing two strings that have both been converted using the casefold() method.
Syntax:
string.casefold()
Parameters: This function doesn’t have any parameters.
Examples:
Example1:
Input:
Given first string = "hello this is BTECHGEEKS" Given second string = "10345 GOOD MORNING btechgeeks"
Output:
The above given first string is : hello this is BTECHGEEKS The given first string after applying casefold() function: hello this is btechgeeks The above given second string is : 10345 GOOD MORNING btechgeeks The given second string after applying casefold() function: 10345 good morning btechgeeks
Example2:
Input:
Given first string = "HeLLO ThiS is BTECHgeeks 12345" Given second string = "GOOD MORNING THIS IS BTECHGEEKS"
Output:
The above given first string is : HeLLO ThiS is BTECHgeeks 12345 The given first string after applying casefold() function: hello this is btechgeeks 12345 The above given second string is : GOOD MORNING THIS IS BTECHGEEKS The given second string after applying casefold() function: good morning this is btechgeeks
Program for casefold() 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 casefold() function to the given first string to get the string in which all of the characters are lower case.
- Store it in another variable.
- Print the above-given String.
- Print the above-given String after applying casefold() function.
- Similarly, do the same for 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 = "hello this is BTECHGEEKS" # Apply casefold() function to the given first string to get the string # in which all of the characters are lower case. # Store it in another variable. rslt_str1 = gvn_fststr.casefold() # Print the above given String print("The above given first string is :", gvn_fststr) # Print the above given String after applying casefold() function. print("The given first string after applying casefold() function:", rslt_str1) # Similarly do the same for other string and print the result string. gvn_scndstr = "10345 GOOD MORNING btechgeeks" rslt_str2 = gvn_scndstr.casefold() print("The above given second string is :", gvn_scndstr) print("The given second string after applying casefold() function:", rslt_str2)
Output:
The above given first string is : hello this is BTECHGEEKS The given first string after applying casefold() function: hello this is btechgeeks The above given second string is : 10345 GOOD MORNING btechgeeks The given second string after applying casefold() function: 10345 good morning btechgeeks
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 casefold() function to the given first string to get the string in which all of the characters are lower case.
- Store it in another variable.
- Print the above-given String.
- Print the above-given String after applying casefold() function.
- Similarly, do the same for 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 casefold() function to the given first string to get the string # in which all of the characters are lower case. # Store it in another variable. rslt_str1 = gvn_fststr.casefold() # Print the above given String print("The above given first string is :", gvn_fststr) # Print the above given String after applying casefold() function. print("The given first string after applying casefold() function:", 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.casefold() print("The above given second string is :", gvn_scndstr) print("The given second string after applying casefold() function:", rslt_str2)
Output:
Enter some Random String = hello all this IS BTECHGEEKS The above given first string is : hello all this IS BTECHGEEKS The given first string after applying casefold() function: hello all this is btechgeeks Enter some Random String = GOOD MORNING THIS IS BTECHGEEKS The above given second string is : GOOD MORNING THIS IS BTECHGEEKS The given second string after applying casefold() function: good morning this is btechgeeks
Go through our tutorial and learn about various Python String Method Examples and learn how to apply the knowledge while dealing with strings.