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
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    lowercase string:   geeksforgeeks
    
  2. Check if a string is palindrome
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # 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")

    chevron_right

    
    

    Output:

    not palindrome
    
  3. Count vowels in a string
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # 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)

    chevron_right

    
    

    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.



My Personal Notes arrow_drop_up


Article Tags :

Be the First to upvote.


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