casefold () string in python



The

String casefold () method is used to implement case-insensitive string matching. It is similar to the string lower () method, but case removes all case differences present in the string. those. 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 to lowercase

    # Python program to convert string to lowercase

    string = "GEEKSFORGEEKS"

     
    # print lowercase string

    print ( "lowercase string: " , string.casefold ())

    Output:

     lowercase string: pythonengineering 
  2. Check if the string is a palindrome

     # A program to check if the string
    # palindrome or not

      
    # change this value for another output

    str = ` pythonengineering`

     
    # make it suitable for case insensitive comparison

    str = str . casefold ()

     
    # flip the line

    rev_str = reversed ( str )

     
    # check if the string is its inverse

    if list ( str ) = = list (rev_str):

    print ( "palindrome " )

    else :

    print ( "not palindrome" )

    Output:

     not palindrome 
  3. Count the vowels in a line

    # Program for counting the number of each
    # vowel string

      
    # vowel string

    v = `aeiou`

     
    # change this value for a different result

    str = `Hello, have you try pythonengineering?`

     
    # user input
    # str = input ( & quot; Enter the string: & quot;)

     
    # case insensitive

    str = str . casefold ()

     
    # make a dictionary with each vowel key and value 0

    c = {}. fromkeys (v, 0 )

     
    # count 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 courtesy of Shivani Bagel . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you`d like to share more information on the topic discussed above.