Skip to content
Related Articles

Related Articles

Improve Article

Python String casefold() Method

  • Last Updated : 05 Aug, 2021

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. 


 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



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 = 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 comparisions
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 :