casefold() string in Python

The casefold() string 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:

string.casefold()
Parameters :
the casefold doesn't take any parameters.
return value:
it return the casefolded string the string converted to lower case.

Examples

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

    Output:

    lowercase string:   geeksforgeeks
    
  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 list(str) == list(rev_str):
          print("palindrome")
    else:
          print(" not palindrome")
    

    Output:

    not palindrome
    
  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
    print(c)
    

    Output:

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

This article is contributed by Shivani Baghel. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



0 Average Difficulty : 0/5.0
No votes yet.










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.