Lower function in Python string

In this Python tutorial, we will see what is a lower function in Python string and its application with examples.

What is the lower() function in Python

In Python, the lower() function is a built-in string method that converts all uppercase characters in a string to lowercase.

Syntax, Parameters, and Return values

It is very important to know that what way we should apply the lower() function in Python, that does not take us to an error.

The syntax:

The syntax of the lower function in Python is:

string.lower()

Parameter:

The Python lower() function does not take any parameters.

Return values:

The Python lower() returns the lowercase string from the given Python string. It converts all the uppercase characters to lowercase and does not affect any characters in the string that are not letters.

Python lower() function using examples

The lower() function in Python has a variety of applications. Here are some practical examples that demonstrate its usage:

Example-1: Convert Python string to Lowercase

The basic application of lower() function in Python is to convert the whole string to lowercase.

For example, Imagine working with a Python string that includes the names of some Texas cities. The data may have been entered inconsistently – some in all caps, some in title case, and some in lowercase.

To bring uniformity to the string in Python, we can use the lower() function to convert all Texas city’s name to lowercase:

texas_cities = "HOuSTon, AUstin, San Antonio, Dallas, foRt Worth"

print(texas_cities.lower())

The output is:

houston, austin, san antonio, dallas, fort worth
Python lower function in String

This way we can string.lower() function to convert all characters of string to lowercase.

Example-2: Case-Insensitive User Input

In some situations where we are taking user input data in Python, user can input their data in any way but for convenience, we should save the data in one format for easy recognition.

Imagine we’re creating a program in Python for a company in California where employees can input the name of their state. We want the program to recognize the state, irrespective of how it is capitalized. We can use the lower() function in Python to achieve this.

state = input("Enter your state:")

state = state.lower()

print('Your state is:', state)

The output is: In this script, no matter how the user enters “Texas” (e.g., “TEXAS”, “texas”, “TeXaS”), it will be converted to lowercase using state.lower() in Python, enabling the program to recognize it correctly.

Enter your state:TExas
Your state is: texas
String lower function in python

This way we can use lower function on string in Python for these case insensitive user input.

Example-3: Sorting list with Python string

Let’s say we are creating a literature application on Python and have a list of famous American authors. We want to display this Python list in alphabetical order to our users, regardless of how the authors’ names were initially capitalized. Here’s how we can use the lower() function on Python string:

authors = ["Ernest Hemingway", "mark twain", "F. Scott Fitzgerald", "JOHN STEINBECK", "Harper Lee"]

authors.sort(key=str.lower)

print(authors)

The output is: A sorted list of authors, regardless of the initial letter casing:

['Ernest Hemingway', 'F. Scott Fitzgerald', 'Harper Lee', 'JOHN STEINBECK', 'mark twain']

The key=str.lower ensures that the sorting function treats all items in the Python list as if they were lowercase, resulting in a truly alphabetical order. By using the lower() function in Python.

Python string lower function for sorting list

This way we can use str.lower() function in Python to sort a list.

Read: Casefold function in Python string

Conclusion

We have seen what is lower() function in Python string, its syntax, parameters and return values. These examples show that the lower() function in Python strings is a powerful tool for ensuring case-insensitive comparison and manipulation of text data.

You may also like to read: