learn how to use python string lower() with example

The lower() method in Python is a built-in string method that returns a new string with all the characters converted to lowercase. It does not modify the original string but instead creates and returns a new string.

Syntax:

string.lower()

How to use the lower() method:

string = "Hello, World!"
lower_string = string.lower()
print(lower_string)

Output:

hello, world!

Example 1

Counting the occurrence of a specific word in a string.

text = "Python is a popular programming language. python has a clean and readable syntax."
word = "python"

count = text.lower().count(word.lower())
print(f"The word '{word}' appears {count} times.")

Output:

The word 'python' appears 2 times.

Example 2

Converting a list of strings to lowercase.

words = ["Apple", "Banana", "Orange", "Grapefruit"]

lowercase_words = [word.lower() for word in words]
print(lowercase_words)

Output:

['apple', 'banana', 'orange', 'grapefruit']

Example 3

Filtering a list based on lowercase search.

fruits = ["Apple", "banana", "Orange", "grapefruit"]
search_term = "apple"

matching_fruits = [fruit for fruit in fruits if search_term.lower() in fruit.lower()]
print(matching_fruits)

Output:

['Apple']

Leave a Reply

Your email address will not be published. Required fields are marked *