Python casefold() function [Tutorial]


Python

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.

 

Deepak Prasad

Deepak Prasad

He is the founder of GoLinuxCloud and brings over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels in various domains, from development to DevOps, Networking, and Security, ensuring robust and efficient solutions for diverse projects. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment