Skip to main content
Python String Casefold Method with Example

Python String Casefold() Method with Example

Python String casefold() is a built-in function used to implement caseless string matching.The casefold() method is very similar to the lower() method in the python. It is used for caseless matching striing in python.

Python String casefold()

The python string casefold() method is used to implement caseless string matching.The casefold() string method is identical to the lower() string technique, only it removes any case distinctions from a string. i.e., while comparing, ignore cases.

Syntax

string.casefold()

Python casefold() doesn’t take any parameter, it returns a string converted in the lower case.

Conver String in lowercase in Python

Let’s create a string in lowercase using python casefold() method.

# test.py
string = "Hi, I'm PYTHONPIP BLOG"
print("Source String:", string)
print("Lowercase String: ", string.casefold())

Output:

Source String: Hi, I'm PYTHONPIP BLOG
Lowercase String:  hi, i'm pythonpip blog

Compare Two strings using casefold()

We can also use casefold() method to compare two strings.

# test.py
s1 = "Hi, I'm PYTHONPIP BLOG"
s2 = "hi, i'm pythonpip blog"
iif(s1.casefold() == s2.casefold()):
    print("Both the strings are same")
else:
    print("Both the strings are not same")

Output:

Both the strings are same

Leave a Reply

Your email address will not be published. Required fields are marked *