Change language

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.

Shop

Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best laptop for development

$499+
Gifts for programmers

Best laptop for Cricut Maker

$299+
Gifts for programmers

Best laptop for hacking

$890
Gifts for programmers

Best laptop for Machine Learning

$699+
Gifts for programmers

Raspberry Pi robot kit

$150

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically