Skip to main content

How to convert a string to lowercase in Python

How to convert a string to lowercase in Python.

Here is a step-by-step tutorial on how to convert a string to lowercase in Python:

Step 1: Start by declaring a string variable that contains the text you want to convert to lowercase. For example, let's use the string "Hello World":

string = "Hello World"

Step 2: Use the built-in lower() function in Python to convert the string to lowercase. This function returns a new string with all the characters in lowercase. Assign the result to a new variable:

lowercase_string = string.lower()

Step 3: You can now use the lowercase_string variable to access the converted lowercase string:

print(lowercase_string)  # Output: hello world

Alternatively, you can directly print the converted lowercase string without assigning it to a separate variable:

print(string.lower())  # Output: hello world

Step 4: If you want to convert a user input string to lowercase, you can use the input() function to prompt the user for input and then convert it to lowercase using the lower() function. Here's an example:

user_input = input("Enter a string: ")
lowercase_input = user_input.lower()
print(lowercase_input)

This code prompts the user to enter a string, converts it to lowercase, and then prints the lowercase version of the input.

Step 5: If you have a list of strings and want to convert each string to lowercase, you can use a loop to iterate over the list and convert each element to lowercase. Here's an example:

strings = ["Hello", "World", "Python"]
lowercase_strings = []

for string in strings:
lowercase_strings.append(string.lower())

print(lowercase_strings) # Output: ['hello', 'world', 'python']

In this code, we define a list of strings, iterate over each string using a loop, convert each string to lowercase, and append the lowercase version to a new list called lowercase_strings. Finally, we print the lowercase_strings list.

That's it! You now know how to convert a string to lowercase in Python using the lower() function.