Python lower() and upper() functions of strings

Master Python string case manipulation with visuals. Dive into the lower() and upper() functions, understanding how they harmonize string cases for diverse programming needs.

Python has built-in functions for converting the case of all letters in a given string.
The functions are:
  • str.lower()
  • str.upper()

These functions are explained below.

Python lower() function

The lower() function is used to convert all cased characters to the lowercase.

The Python lower() function returns a copy of the given string in which all case letters are converted to lowercase.

Syntax of lower() function

This is how case letters can be converted to lowercase:

Str.lower()

The Str can be any string that needs to be converted to lowercase.

An example of Python lowercase

In the following example, a string is created with all capital letters. This is followed by using the lower() function and the returned string is displayed by using the print function.

#lower() function demo

str_lower = "A LAZY FOX";
print ("String with lower()", str_lower.lower())
print ("The orginal string:" ,str_lower)

Output:

String with lower() a lazy fox
The original string: A LAZY FOX

In the output, you can see all letters are lowercase.

The second print function displays the original string that shows the string remains the same after using the lower() function; as such the strings are immutable.

An example of lower() with input function

For this example, the input function is used for taking the user input. As you enter a string and press enter (return), the lower() function will execute and if the entered string contains any capital letters, those will be converted to lowercase. Have a look:

#lower() with input() demo


str_input = input("Enter a string:  ")

print ("String with lower() = ", str_input.lower())
print ("The orgginal string:" ,str_input)

Sample output:

lowercase input

The upper() function

The upper() function returns a copy of a given string with all the small case characters converted to upper case.

See this example where the input function is used to get the user input just like the above example.

#an example of upper() function

str_upper = input("Enter a string: ")
print ("Copy of string after upper() = ", str_upper.upper())
print ("The orgginal string:" ,str_upper)

upper

You can see that mixed letters were entered and the upper() function converted all small letters to upper-case letters.

The original string remained the same, as such upper() function returns a copy of the string.
Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we unravel the mysteries of coding together!