Python:
Converting a string to lower case
How to:
Lowercasing a string in Python is simple with the .lower()
method.
original_string = "Hello, World!"
lowercase_string = original_string.lower()
print(lowercase_string) # Output: hello, world!
Or use a list comprehension for more control:
s = "HELLO, World!"
lower_list = [char.lower() for char in s]
print(''.join(lower_list)) # Output: hello, world!
Deep Dive
The .lower()
method has been a part of Python’s string type since pretty early on. It’s a straightforward way to ensure case-insensitive data processing, which is useful in situations like case-insensitive user inputs.
There are alternatives, such as using regular expressions:
import re
s = "HELLO, World!"
lower_s = re.sub(r'[A-Z]', lambda match: match.group(0).lower(), s)
print(lower_s) # Output: hello, world!
But this is overkill for simply converting a string to lowercase.
Under the hood, Python’s .lower()
relies on Unicode character mapping. Unicode standard specifies the lowercase equivalent of almost all characters that have a case. This process is more complex than just subtracting a value to get from ‘A’ to ‘a’ because not all languages and scripts have such a simple and direct mapping.
See Also
- The Python documentation on string methods: https://docs.python.org/3/library/stdtypes.html#string-methods
- Unicode case mapping details: https://www.unicode.org/reports/tr21/tr21-5.html
- A tutorial on Python list comprehensions: https://realpython.com/list-comprehension-python/