In this article, we will discuss about casefold function in python. As we know that string is a set of characters. In Python, if we want to convert all the characters in a string to lower case, then using casefold()
is the best option.
Using Python casefold() function
casefold() in python is used to convert all the characters in the string to lowercase.
Syntax:
string.casefold()
It won't take any parameter.
Return:
Return the string in lowercase.
Example 1:
Python program to create a string with all uppercase characters and convert into lowercase.
# Consider a string
my_string1= "GOLINUXCLOUD"
# Actual string
print("Actual string: ",my_string1)
# Lowercase string
print("Lowercase string:",my_string1.casefold())
Output:
Actual string: GOLINUXCLOUD
Lowercase string: golinuxcloud
Explanation:
We created a string -Â "GOLINUXCLOUD". After applying the casefold function, the string is - "golinuxcloud".
Example 2:
Python program to create a string with all uppercase characters and convert into lowercase.
# Consider a string
my_string1= "GOLINUXCLOUD IS THE BEST SITE FOR STUDENTS"
# Actual string
print("Actual string: ",my_string1)
# Lowercase string
print("Lowercase string:",my_string1.casefold())
Output:
Actual string: GOLINUXCLOUD IS THE BEST SITE FOR STUDENTS
Lowercase string: golinuxcloud is the best site for students
Explanation:
We created a string - "GOLINUXCLOUD IS THE BEST SITE FOR STUDENTS". After applying the casefold function, the string is - "golinuxcloud is the best site for students".
Example 3:
Python program to create a list with 6 strings and convert all of them to lowercase using casefold function.
# Consider 6 strings in a list.
my_list1= ["GOLINUXCLOUD","IS", "BEST", "SITE", "FOR", "STUDENTS"]
# Actual list
print("Actual list of strings: ",my_list1)
# Lowercase strings from the list
print("Lowercase strings:")
# Convert each element in a list to lowercase
for i in my_list1:
print(i.casefold())
Output:
Actual list of strings: ['GOLINUXCLOUD', 'IS', 'BEST', 'SITE', 'FOR', 'STUDENTS']
Lowercase strings:
golinuxcloud
is
best
site
for
students
Explanation:
We created a list that hold 6 strings in uppercase. By using for loop, we converted each string into lowercase by using casefold function and displayed one by one. Here 'i' is an iterator to iterate each string in a list.
Example 4:
Python program to create a tuple with 6 strings and convert all of them to lowercase using casefold function.
# Consider 6 strings in a tuple.
my_tuple1= ("GOLINUXCLOUD","IS", "BEST", "SITE", "FOR", "STUDENTS")
# Actual tuple
print("Actual tuple of strings: ",my_tuple1)
# Lowercase strings from the tuple
print("Lowercase strings:")
# Convert each element in a tuple to lowercase
for i in my_tuple1:
print(i.casefold())
Output:
Actual tuple of strings: ('GOLINUXCLOUD', 'IS', 'BEST', 'SITE', 'FOR', 'STUDENTS')
Lowercase strings:
golinuxcloud
is
best
site
for
students
Explanation:
We created a tuple that hold 6 strings in uppercase. By using for loop, we converted each string into lowercase by using casefold function and displayed one by one. Here 'i' is an iterator to iterate each string in a tuple.
Summary
From this guide, we seen how to use casefold function to convert all characters in a string to lowercase with four different examples. Based on your requirement, you can use this on any kind of data structures like list, tuple etc. If the string is already in lowercase, then the same string is returned. The casefold function is similar to lower function. but it is faster and flexible than lower() in terms of internal processing.