How to Convert Strings to Lowercase in Python?

Estimated read time 1 min read

In Python, you can convert strings to lowercase using the lower() method or the str.lower() function. Here’s how you can do it:

Using the lower() method:

string = "Hello, World!"
lowercase_string = string.lower()
print(lowercase_string)  # Output: "hello, world!"

In this example, the lower() method is called on the string variable, which returns a new string with all characters converted to lowercase. The resulting lowercase string is then assigned to the lowercase_string variable and printed.

Using the str.lower() function:

string = "Hello, World!"
lowercase_string = str.lower(string)
print(lowercase_string)  # Output: "hello, world!"

In this example, the str.lower() function is called with the string variable as an argument. It returns a new string with all characters converted to lowercase, which is then assigned to the lowercase_string variable and printed.

Both the lower() method and the str.lower() function provide the same result of converting a string to lowercase. The choice between them depends on whether you prefer the method syntax or the function syntax.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply