How to Convert Strings to Lowercase in Python

python lowercase

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

The syntax for the lower() method is as follows:

string.lower()

Here, string is the string that we want to convert to lowercase.

How to Use the lower() Method in Python

To use the lower() method in Python, we first need to have a string that we want to convert to lowercase. Let’s take a look at some examples.

Example 1: Convert a String to Lowercase

string = "HELLO WORLD"
lowercase_string = string.lower()
print(lowercase_string)

Output:

hello world

In this example, we have a string string that is in uppercase. We use the lower() method to convert it to lowercase and assign the result to a new variable lowercase_string. Finally, we print the new string to the console.

Example 2: Convert User Input to Lowercase

string = input("Enter a string: ")
lowercase_string = string.lower()
print("Lowercase string:", lowercase_string)

Output:

Enter a string: HeLLo WoRLd
Lowercase string: hello world

In this example, we take user input using the input() function and store it in the string variable. We then use the lower() method to convert the string to lowercase and store the result in the lowercase_string variable. Finally, we print the lowercase string to the console.

Example 3: Convert a List of Strings to Lowercase

strings = ["HELLO", "WORLD"]
lowercase_strings = [string.lower() for string in strings]
print(lowercase_strings)

Output:

['hello', 'world']

In this example, we have a list of strings strings. We use a list comprehension to apply the lower() method to each string in the list and store the resulting lowercase strings in a new list lowercase_strings. Finally, we print the new list to the console.

Example 4: Convert a Dictionary of Strings to Lowercase

dictionary = {"Key1": "VALUE1", "Key2": "VALUE2"}
lowercase_dictionary = {key.lower(): value.lower() for key, value in dictionary.items()}
print(lowercase_dictionary)

Output:

{'key1': 'value1', 'key2': 'value2'}

In this example, we have a dictionary of strings dictionary. We use a dictionary comprehension to apply the lower() method to each key and value in the dictionary and store the resulting lowercase keys and values in a new dictionary lowercase_dictionary. Finally, we print the new dictionary to the console.

Example 5: Convert a String to Lowercase and Count Occurrences of a Substring

string = "HELLO WORLD, hello Python"
lowercase_string = string.lower()
count = lowercase_string.count("hello")
print("Lowercase string:", lowercase_string)
print("Count of 'hello':", count)

Output:

Lowercase string: hello world, hello python
Count of 'hello': 2

In this example, we have a string string that contains two occurrences of the substring “hello” (ignoring the case). We use the lower() method to convert the string to lowercase and store the result in the lowercase_string variable. We then use the count() method to count the number of occurrences of the substring “hello” in the lowercase string and store the result in the count variable. Finally, we print both the lowercase string and the count to the console.

Conclusion

In this article, we have explored how to use the lower() method in Python to convert strings to lowercase. We have seen how to use this method to convert individual strings, user input, lists of strings, dictionaries of strings, and how to count occurrences of a substring in a lowercase string. By understanding how to use the lower() method, you can manipulate strings in Python with ease.