Python supports the case fold concept. Case fold is a concept of converting all of the characters in a document to the same case, either all upper case or all lower case, to speed up comparisons during the indexing process. Python casefold() string function is suggested to be used during such situations.
Definition
- Python casefold() built-in function is an aggressive lower() function that converts strings to case folded strings for caseless matching.
- The Python casefold() string method returns a string in which all characters are lower case.
Python string casefold()
This function belongs to the string class and can be used only on the string objects. The casefold() method in Python is used for case-insensitive string comparison. It is similar to the lower() string technique, except that case removes all case distinctions from a string. In other words, when comparing, disregard instances.
Consider an example of the German lowercase letter ‘ß’, which is equal to ‘ss’. However, because ‘ß’ is already in lowercase, the lower() function has no effect on it. Python’s string casefold() function, on the other hand, turns it to ‘ss’.
casefold() Syntax
Python casefold() function follows the following syntax:
string.casefold()
Parameters for casefold()
The casefold() function does not accept any parameter.
Return value from casefold()
It returns the case folded string, which is the string that has been changed to lower case.
Example 1: Lowercase using casefold()
Example
# Python program to illustrate casefold()
mystr = 'PYTHON PROGRAMMING'
# print lowercase string
print('Original Value:', mystr)
print('Lowercase string:', mystr.casefold())
# symbols and numbers are unaffected
str1 = '12@$ HellO'
print('Lowercase string:', str1.casefold())
Output
Original Value: PYTHON PROGRAMMING
Lowercase string: python programming
Lowercase string: 12@$ hello
For the English alphabet, this string casefold() function turns all uppercase letters into lowercase letters. But what if the string contains characters from a different language and in a different encoding? This issue is solved using the Python string casefold() function.
Example 2: Comparison using casefold()
The Python casefold() function’s strength is that it not only converts to lowercase but also strictly converts.
Example
# Python program to illustrate casefold()
str1 = 'ßack to YOU'
print('Lowercase string:', str1.casefold())
str2 = 'ssack to YOU'
# ß is equivalent to ss
if str1.casefold() == str2.casefold():
   print('The strings are equal')
else:
   print('The strings are not equal')
Output
Lowercase string: ssack to you
The strings are equal
casefold() vs lower()
Python lower() function converts all capital characters in a string to lowercase characters and returns the result. So what exactly is the difference between casefold() and lower() function. From the above programs, the casefold() function appears to be the same as the string lower() function. In fact, the effect is the same when the string is made up of ASCII characters. Case folding, on the other hand, is significantly more aggressive and aims to erase all case distinctions in a string. Let us understand this by looking at the following example:
Example
x = 'Descßa'
print(x.casefold())
x = 'Descßa'
print(x.lower())
Output
descssa
descßa
If we used the lower() function on this alphabet, nothing would happen because the letter is already in lowercase, therefore the output would be ‘ ß ‘. Python string casefold() not only transforms to lowercase but also ensures that we get our English string “ss” back.
Note – However, if you’re trying to normalize text from languages other than English, use casefold() to compare your strings for more consistent results.
Frequently Asked Questions
Q1. What does Casefold do in Python?
Case fold is a concept of converting all of the characters in a document to the same case, either all upper case or all lower case, to speed up comparisons during the indexing process. Python casefold() built-in function is an aggressive lower() function that converts strings to case folded strings for caseless matching. This function belongs to the string class and can be used only on the string objects. The casefold() method in Python is used for case-insensitive string comparison.
Q2. How do you capitalize in Python?
There are 2 distinct functions in the Python string class that are used to capitalize texts. They are:
- Python string capitalize() – This function in Python turns the first character of a string to an uppercase letter and converts any remaining characters to lowercase.
- Python string upper() – The upper() method returns a string that has all lowercase characters converted to uppercase characters.
Example
mystr = 'I love Python Programming'
print('Original string:', mystr)
print('Capitalize function:', mystr.capitalize())
print('Upper function:', mystr.upper())
Output
Original string: I love Python Programming
Capitalize function: I love python programming
Upper function: I LOVE PYTHON PROGRAMMING
Q3. How do you center in Python?
The Python center() function returns a padded string with the specified character. This function is used to center a string by filling paddings to the left and right of the string. This method accepts two parameters, the first of which is a width and the second of which is an optional fillchar. The fillchar is a character that is used to fill the string’s left and right padding.
Syntax:
string.center(width, fillchar)
where,
width – length of the string with padded characters
fillchar (Optional) – character to be padded. Default value is space.
Example
mystr = 'Programming'
new_str1 = mystr.center(20)
new_str2 = mystr.center(20, '*')
print('Centered string:', new_str1)
print('Centered string with fillchar:', new_str2)
Output
Centered string:Â Â Â Â Programming
Centered string with fillchar: ****Programming*****
Leave a Reply