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:
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:
The original string: A LAZY FOX
In the output, you can see all letters are lowercase.
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:
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)
You can see that mixed letters were entered and the upper() function converted all small letters to upper-case letters.