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:

  • Casefolding converts non-lowercase characters to lowercase in most cases - except scenarios like Cherokee lowercase letters, which are converted to uppercase.
  • Finding whether two strings of characters match, is a common requirement in information processing. Often the texts to be compared may not be of the same case. To perform a case insensitive matching of strings it is first required to convert all the characters to a uniform case.  
  • The method casefold() does this - makes all the characters of a given string either lowercase or uppercase.
  • Note that in Python strings are Unicode code points of the characters which are flexibly represented using one of the encoding schemes like UTF-8, UTF-16 and so on. 
  • While calling lower() results in simple mapping to lower case letters and calling upper() results in simple mapping to upper case letters calling casefold() results in removing the case as per Unicode standards (including the stability property and case tailoring as per Unicode standards).
  • An example of the uppercase conversion during case folding happens for Cherokee lowercase letters. The Cherokee pronunciation YO is represented as the letter "Ᏺ" and defined with the code point U+13F2. The smaller case version of the letter is represented as "ᏺ" for which the code point is U+13FA. The casefold() method converts the string "Ᏺᏺ" to "ᏲᏲ" which has all the letters  in uppercase.

Example 1:

# Example Python program that applies case folding 
# to English text strings which removes case
# (from uppercased, lowercased, titlecased and mixedcased
#  strings)

# From all Upper case
engString = "CHAPTER ONE";
print("Before casefolding an uppercased string: %s"%engString);
print("Result of casefolding an uppercased string: %s"%engString.casefold());
print("---");

# From all lower case
engString = "typographical";
print("Before casefolding a lowercased string: %s"%engString);
print("Result of casefolding an lowercased string: %s"%engString.casefold());
print("---");

# From title case
engString = "Chapter Two";
print("Before casefolding a titlecased string: %s"%engString);
print("Result of casefolding a titlecased string: %s"%engString.casefold());
print("---");

# From mixed case
engString = "How exquisitely the individual Mind";
print("Before casefolding a mixedcased string: %s"%engString);
print("Result of casefolding a mixedcased string: %s"%engString.casefold());
print("---");

Output:

Before casefolding an uppercased string: CHAPTER ONE

Result of casefolding an uppercased string: chapter one

---

Before casefolding a lowercased string: typographical

Result of casefolding an lowercased string: typographical

---

Before casefolding a titlecased string: Chapter Two

Result of casefolding a titlecased string: chapter two

---

Before casefolding a mixedcased string: How exquisitely the individual Mind

Result of casefolding a mixedcased string: how exquisitely the individual mind

---

Example 2 - When return values of casefold() and lower() differ:

# Example Python program that compares two strings from the German language
# "Weiß" and "weiß" after transforming using lower() and casefold() methods

# Strings representing colors
color1 = "Weiß";
color2 = "weiß";

# Compare two colors and print the results
simpleCompare = (color1 == color2);
print("Strings %s and %s match:%s"%(color1, color2, simpleCompare));

# Perform a caseless comparison by converting both the strings to lowercase
lowercaseComparison = (color1.lower() == color2.lower());
print("Strings %s and %s match:%s"%(color1.lower(), color2.lower(), lowercaseComparison));

# Perform a caseless comparison by mixing lowercase and casefold results
lowerVsCasefold = (color1.lower() == color2.casefold());
print("Strings %s and %s match - lower() vs casefold():%s"%(color1.lower(), color2.casefold(), lowerVsCasefold));

# Perform a caseless comparison by typecasing both the strings
typecasedMatch = (color1.casefold() == color2.casefold());
print("Strings %s and %s match - casefold() vs casefold():%s"%(color1.casefold(), color2.casefold(), typecasedMatch));

Output:

Strings Weiß and weiß match:False

Strings weiß and weiß match:True

Strings weiß and weiss match - lower() vs casefold():False

Strings weiss and weiss match - casefold() vs casefold():True

Example 3 - When the return value of casefold() is in uppercase:

# Example python program that does casefolding for a 
# Cherokee string
cherokeeLetters = "Ᏺᏺ";

# Case folding results in all upper case letters
print("Cherokee string after case fold:");
print(cherokeeLetters.casefold());

print("Unicode representation before and after applying casefolding:");
print(cherokeeLetters.encode().hex("-"));
print(cherokeeLetters.casefold().encode().hex("-"));

Output:

Cherokee string after case fold:

ᏲᏲ

Unicode representation before and after applying casefolding:

e1-8f-b2-e1-8f-ba

e1-8f-b2-e1-8f-b2


Copyright 2024 © pythontic.com