What is a string in Python?
Strings in Python 3 hold Unicode text rather than bytes (in Python 2 the "str" type was a byte string). Text and bytes are separate types, and converting between them is always explicit. String literals can either be enclosed in double-quotes or single quotes. You can use single quotes inside double quotes and vice versa. Python strings are not mutable; once they are created, they cannot be changed. Instead of modifying the existing string, the string manipulation functions will return a new one. With the built-in Python "str" library, you can search, concatenate, reverse, split, and compare strings.
Python String lower() Method Syntax
Following is the syntax of string.lower() method:
Where:
- string: the string to be converted to lowercase
The following is an example of converting a string to lowercase in Python:
How to convert certain characters of a string to lowercase in Python?
To convert certain characters in a string to lowercase, you can use the range operator and the string.lower() function. The following is an example of converting the first two characters of a string to lower case:
Converting a Python string to lowercase using the casefold() method
The string.casefold() method is similar to the string.lower() method, but it is more powerful. The following is an example of converting a string to lowercase using the string.casefold() method in Python:
Unlike the string.lower() method, which can only convert ASCII characters, the string.casefold() method can convert characters from other languages to including Unicode. For example, the German letter "ß" is already lowercase, so the lower() method does not perform the conversion. But the casefold() method converts "ß" to its equivalent character "ss". Below is an example of comparing the casefold() and lower() methods.
How to convert a Python string to lowercase using a for loop?
The following is an example of converting a Python string to lowercase using a for loop: