Skip to content
Related Articles

Related Articles

Python String casefold() Method

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 03 May, 2022

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. 




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


# Python program to convert string in lower case
# print lowercase string
print("lowercase string: ",string.casefold())


lowercase string: geeksforgeeks

Example 2: Check if a string is palindrome 


# Program to check if a string
#  is palindrome or not
# change this value for a different output
str = 'geeksforgeeks'
# make it suitable for caseless comparison
str = str.casefold()
# reverse the string
rev_str = "".join(reversed(str))
# check if the string is equal to its reverse
if str == rev_str:
      print("not palindrome")


not palindrome

Example 3: Count Vowels in a String 


# Program to count
# the number of each
# vowel in a string
# string of vowels
v = 'aeiou'
# change this value for a different result
str = 'Hello, have you try geeksforgeeks?'
# input from the user
# str = input("Enter a string: ")
# caseless comparison
str = str.casefold()
# make a dictionary with
# each vowel a key and value 0
c = {}.fromkeys(v,0)
# count the vowels
for char in str:
          if char in c:
                  c[char] += 1


{'a': 1, 'e': 6, 'i': 0, 'o': 3, 'u': 1}

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!