Python String casefold() Method
Python String casefold() method is used to implement caseless string matching. It is similar to lower() string method but case removes all the case distinctions present in a string. i.e ignore cases when comparing.
Syntax:
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
string.casefold()
Parameters:
The casefold() method doesn’t take any parameters.
Return value:
It returns the case folded string the string converted to lower case.
Example 1: Convert String in Lower Case
Python3
# Python program to convert string in lower casestring = "GEEKSFORGEEKS" # print lowercase stringprint("lowercase string: ",string.casefold()) |
Output:
lowercase string: geeksforgeeks
Example 2: Check if a string is palindrome
Python3
# Program to check if a string# is palindrome or not # change this value for a different outputstr = 'geeksforgeeks' # make it suitable for caseless comparisonstr = str.casefold() # reverse the stringrev_str = reversed(str) # check if the string is equal to its reverseif str == rev_str: print("palindrome")else: print("not palindrome") |
Output:
not palindrome
Example 3: Count Vowels in a String
Python3
# Program to count # the number of each # vowel in a string # string of vowelsv = 'aeiou' # change this value for a different resultstr = 'Hello, have you try geeksforgeeks?' # input from the user# str = input("Enter a string: ") # caseless comparisionsstr = str.casefold() # make a dictionary with # each vowel a key and value 0c = {}.fromkeys(v,0) # count the vowelsfor char in str: if char in c: c[char] += 1print(c) |
Output:
{'a': 1, 'e': 6, 'i': 0, 'o': 3, 'u': 1}


