Is there a way to convert a string to lowercase? "Kilometers" → "kilometers" See How to change a string into uppercase? for the opposite.
Yes, you can convert a string to lowercase in Python using the lower() method or the str.lower() function. Here's how to do it with examples:
Using the lower() Method:
The lower() method is a built-in method of string objects in Python. It returns a new string with all the alphabetic characters converted to lowercase.
python
original_string = "Kilometers"
lowercase_string = original_string.lower()
print(lowercase_string)
Output:
kilometers
Using the str.lower() Function:
You can also use the str.lower() function, which takes a string as an argument and returns a new string with all alphabetic characters converted to lowercase.
python
original_string = "Kilometers"
lowercase_string = str.lower(original_string)
print(lowercase_string)
Output:
kilometers
Both approaches achieve the same result: converting the original string to lowercase. You can choose the method that you find more readable or convenient in your code.
Comments
Post a Comment