Python String casefold()

Python String casefold() method is used to implement caseless string matching. Case folding is similar to lowercasing but more aggressive because the casefold() function is more aggressive as it converts all string characters to lowercase. It is intended to remove all case distinctions in a string.

casefold() Syntax

The syntax of casefold() method is:

string.casefold()

casefold() Parameter

The casefold() function does not take any parameters.

casefold() Return Value

The casefold() function returns a copy of the case folded string, i.e., the string is converted to lowercase. It doesn’t modify the original string.

Difference between casefold and lower in Python

The lower() method converts all the uppercase characters in a string to lowercase characters, while the casefold() method converts all the string characters into lowercase. In General, the casefold() method removes all case distinctions present in a string.

For example, the German lowercase letter ‘ß‘ is equivalent to “ss“. Since ‘ß‘ is already lowercase, the lower() method would do nothing to ‘ß‘; however, casefold() still converts it to “ss“.

Example 1: Convert string to lowercase using casefold()

text = "PYTHON CASEFOLD EXAMPLE" 

# Prints the lowercase string 
print ("Lowercase string is:", text.casefold())

Output

Lowercase string is: python casefold example

Example 2: Compare strings using casefold()

str1 = "Pythonß" 
str2 = "Pythonss" 

# ß in german is equivalent to ss
if str1.casefold() == str2.casefold():
    print('The given strings are equal.')
else:
    print('The given strings are not equal.')

Output

The given strings are equal.
Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Get notified of the best deals on our WordPress themes.

You May Also Like
Python Write Text File

Python Write Text File

Table of Contents Hide Steps on How to write to a File in Python Python open() function Methods for Writing to a text file in PythonPython close() functionExamples for Writing to…
View Post
Numpy.argmax() In Python

numpy.argmax() in Python

Table of Contents Hide SyntaxParametersReturn ValueFinding the maximum element from a matrix with Python numpy.argmax()Using np.unravel_index on argmax outputFinding Maximum Elements along columns using Python numpy.argmax() The numpy.argmax() function returns the indices…
View Post
Importerror No Module Named Requests

ImportError: No module named requests

In Python, if you try to import Requests without installing the module using pip, you will get ImportError: no module named requests error.  In this tutorial, let’s look at installing…
View Post
Python Ascii()

Python ascii()

Table of Contents Hide ascii() Syntax ascii() Parametersascii() Return ValueExample 1: How ascii() method works?Example 2: ascii() vs print() The ascii() in Python is a built-in function that returns a printable…
View Post