Casefold() Method Of Str Class In Python

Method Name:

casefold

Method Signature:

casefold()

Method Parameters:

None

Return Value:

Returns a casefolded copy of the string.

Method Overview:

  • The casefold() method of Python str class returns a casefolded version of the original string.
  • The method casefold() is more aggressive during conversion of the characters than the lower() method in order to provide consistent results while comparing non-English texts.

 

Example:

# Define two strings

color1 = "weiß"

color2 = "weiß"

 

print("String value after conversion using lower():%s",color1.lower())

print("String value after conversion using casefold():%s",color1.casefold())

 

# The condition evaluates to false

if color1.lower() == color2.casefold():

    print("Yes") # This will not be printed

 

# The condition evaluates to true

if color1.casefold() == color2.casefold():

    print("Casefolding is done more aggressively")

Output:

String value after conversion using lower():%s weiß

String value after conversion using casefold():%s weiss

Casefolding is done more aggressively


Copyright 2018 © pythontic.com