Python str.lower() function


str.lower() Function in Python

The str.lower() function in Python is used to convert all uppercase letters in a string to lowercase. Non-alphabetical characters (such as digits, punctuation, and spaces) remain unaffected.

Syntax:

string.lower()
  • string: The string that you want to convert to lowercase.

Example:

# Example 1: Convert a string to lowercase my_string = "HELLO, WORLD!" lowercase_string = my_string.lower() print(lowercase_string) # Output: "hello, world!"

In this example:

  • The string "HELLO, WORLD!" is converted to "hello, world!".
  • Only the uppercase letters are converted to lowercase, while punctuation and spaces are unaffected.

Example with mixed case:

# Example 2: Mixed case string mixed_string = "Python Is Fun!" lowercase_mixed = mixed_string.lower() print(lowercase_mixed) # Output: "python is fun!"

Here, the uppercase letters in "Python Is Fun!" are converted to lowercase, and other characters (like spaces and punctuation) remain unchanged.

Example with numbers and special characters:

# Example 3: String with numbers and special characters special_string = "123ABC!@#" lowercase_special = special_string.lower() print(lowercase_special) # Output: "123abc!@#"

In this case:

  • The digits (123) and special characters (!@#) are not affected.
  • The uppercase letters (ABC) are converted to lowercase (abc).

Key Points:

  • str.lower() affects only alphabetic characters and converts them to lowercase.
  • It does not modify the original string but returns a new string with the converted characters.
  • Non-alphabetic characters like numbers, punctuation, and spaces remain unchanged.