Python string casefold() method

Python String casefold() Method

Description

The casefold() method is a new addition to string methods. The .casefold() method returns a copy of a string with all characters converted to lowercase. It is similar to .lower(), but .casefold() can handle Unicode characters, while .lower() only works with ASCII text.

Syntax

The syntax of the casefold() method is as follows:

var.casefold()

Parameters

None

Return Value

This method returns a copy of the string with all characters in lowercase.

Example

The following example demonstrates the use of the casefold() method.

var = "THIS IS STRING EXAMPLE....WOW!!!"
var1 = var.casefold()
print ("Original string:", var)
print ("Converted to lowercase:", var1)

This will generate the following output −

Original string: THIS IS STRING EXAMPLE....WOW!!!
Converted to lowercase: this is string example....wow!!!

Unicode example −

var = "Straße"
var1 = var.casefold()

print ("Original string:", var)
print ("Converted to lowercase:", var1)

This will generate the following output −

Original string: Straße
Converted to lowercase: strasse

The German character ß is converted to “ss”.

Leave a Reply

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