Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like 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. 

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 case
string = "GEEKSFORGEEKS"
  
# print lowercase string
print("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 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("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 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
print(c)

Output:

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



My Personal Notes arrow_drop_up
Recommended Articles
Page :