Converting String to lowercase in Python

To convert a string to lowercase in Python, you can use one of the built-in string.lower() or string.casefold() methods. The string.lower() method will convert all uppercase characters and return a copy of the provided string with all characters converted to lowercase. The string.lower() method will return the original string if there are no uppercase characters. The string.casefold() method also converts the string to lowercase but compared to the string.lower() method, the string.casefold() method is more aggressive. This means that the string.casefold() method will convert more characters to lowercase the string.lower(). You can also manually convert a string to lowercase using the "for loop". In this Python Lowercase String example, we use the string.lower() method. Below are more examples of converting a string to lower case with detailed descriptions. Click Execute to run Python Lowercase String Example online and see the result.
Converting String to lowercase in Python Execute
my_str = "HELLO, WORLD!"

print(my_str.lower())
Updated: Viewed: 1286 times

What is a string in Python?

Strings in Python 3 are 16-bit Unicode bytes (and 8-bit ASCII bytes in Python 2). Every character is represented by a single byte. 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:

Python String lower() Syntax
string.lower()

Where:
  • string: the string to be converted to lowercase

The following is an example of converting a string to lowercase in Python:

Python String to Lowercase Example
print('PYTHON'.lower())

# output: 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:

Python Lowercase First Characters String Example
my_str = "PYTHON"

print(my_str[0:2].lower() + my_str[2:])

# output: pyTHON

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:

Python String casefold() Example
my_str = "PYTHON is BeST! PYTHON Is EASY!"

print(my_str.casefold())

# output: python is best! python is easy!

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.

Python Сomparing the casefold() and lower() Example
my_str = 'groß'

print('lower():', my_str.lower())

#output: groß

print('casefold():', my_str.casefold())

#output: gross

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:

Python String to Lowercase using For Loop
string = 'All CHARACTERS WIlL bE CONverTED'
string1 = ''

for i in string:
  if(ord(i) >= 65 and ord(i) <= 90):
    string1 = string1 + chr((ord(i) + 32))
  else:
    string1 = string1 + i
 
print(string1)
# output: all characters will be converted

See also