In this python tutorial, you will learn how to convert a string to lowercase.
Table Of Contents
Introduction
A string is a set of character. If all the characters in the string are of lower case, then we can say that string is in lowercase. Let’s discuss some approaches to convert a string to lowercase in python.
Check the case of a String in Python
First, we have to check whether string is lower or upper. So this can be done by using the islower() method. If all the characters in the string are in lower case, it will return True, otherwise, it will return False.
Syntax:
Frequently Asked:
input_str.islower()
Where the input_str is the input string.
Example 1:
In this example, we will check whether the string is in lower case or not.
# Consider the below string input_str="WELCOME TO THISPOINTER" # Display the actual string print("Actual String: ",input_str) # Check the status print("Is it Lower?: ",input_str.islower())
Output:
Latest Python - Video Tutorial
Actual String: WELCOME TO THISPOINTER Is it Lower?: False
We can see that the string is in the upper case. So it returned False.
Example 2:
In this example, we will check whether the string is in lower case or not.
# Consider the below string input_str="welcome to thispointer" # Display the actual string print("Actual String: ",input_str) # Check the status print("Is it Lower?: ",input_str.islower())
Output:
Actual String: welcome to thispointer Is it Lower?: True
We can see that the string is in lower case. So it returned True.
Convert a string to lowercase using lower()
The lower() method of string class will convert each character in the string to lower case.
Syntax:
input_str.lower()
Where input_str is the input string.
Example 1:
In this example, we will convert the string – “Welcome to ThisPointer” to lower case.
# Consider the below string input_str="Welcome to ThisPointer" # Display the actual string print("Actual String: ",input_str) # Check the status print("is it Lower:?",input_str.islower()) # Convert to lower case converted=input_str.lower() print("Lower case: ",converted) # Check the status print("is it Lower:?",converted.islower())
Output:
Actual String: Welcome to ThisPointer is it Lower:? False Lower case: welcome to thispointer is it Lower:? True
We can see that all the characters in the input_str to lower case.
Example 2:
In this example, we will convert the string – “WELCOME TO THISPOINTER” to lower case.
# Consider the below string input_str="WELCOME TO THISPOINTER" # Display the actual string print("Actual String: ",input_str) # Check the status print("is it Lower:?",input_str.islower()) # Convert to lower case converted=input_str.lower() print("Lower case: ",converted) # Check the status print("is it Lower:?",converted.islower())
Output:
Actual String: WELCOME TO THISPOINTER is it Lower:? False Lower case: welcome to thispointer is it Lower:? True
We can see that all the characters in the input_str to lower case.
Convert a string to lowercase using casefold()
The casefold() method will convert each character in the string to lower case.
Syntax:
input_str.casefold()
Where input_str is the input string.
Example 1:
In this example, we will convert the string – “Welcome to thisPointer” to lower case.
# Consider the below string input_str="Welcome to thisPointer" # Display the actual string print("Actual String: ",input_str) # Check the status print("is it Lower:?",input_str.islower()) # Convert to lower case converted=input_str.casefold() print("Lower case: ",converted) # Check the status print("is it Lower:?",converted.islower())
Output:
Actual String: Welcome to thisPointer is it Lower:? False Lower case: welcome to thispointer is it Lower:? True
We can see that all the characters in the input_str to lower case.
Example 2:
In this example, we will convert the string – “WELCOME TO THISPOINTER” to lower case.
# Consider the below string input_str="WELCOME TO THISPOINTER" # Display the actual string print("Actual String: ",input_str) # Check the status print("is it Lower:?",input_str.islower()) # Convert to lower case converted=input_str.casefold() print("Lower case: ",converted) # Check the status print("is it Lower:?",converted.islower())
Output:
Actual String: WELCOME TO THISPOINTER is it Lower:? False Lower case: welcome to thispointer is it Lower:? True
We can see that all the characters in the input_str to lower case.
Summary
In this article, you learned how to convert all the characters in the string to lowercase using lower() and casefold() methods. It is a good idea to check first whether the string is in lower case or not. It is done by using the islower() function.
Happy Coding
Latest Video Tutorials