Convert a String to Lowercase in Python

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:

Advertisements
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:

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

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top