How to Convert String to Lowercase in Python

Many times we need to lowercase tons of data which is available in string format. If we try to it manually it will take hours or maybe days. So, we need some method or function which will convert string to lowercase in python.
example usernames of a login system.

We can Convert String to Lowercase in Python with the help of inbuilt string method lower(). The string lower() method converts all uppercase characters in a string into lowercase characters and returns it. And in the given string If there is no uppercase characters exist, it returns the original string.

Syntax to Convert String to Lowercase in Python

The syntax of the lower() method is:

str.lower()

Here str can be any string whose lowercase you want.

Parameters of String lower() Method in Python

The lower() method in python doesn’t take any parameters.

Return Value of lower() Method

The lower() method in Python returns the lowercased string from the given string. It converts all uppercase characters to lowercase.

If no uppercase characters exist, lower() will return the original string.

Python lower() Method Compatibility

Python 2.xYes
Python 3.xYes

Python Programs to Convert Uppercase String to Lowercase Using lower()

Enough talks, now let’s jump straight into the python programs to convert the string to lowercase. Here we are using Python lower() function which converts all the string to lowercase. This is the simple method of changing camel case characters to small letter characters.

Example 1: Basic Program to Convert String to Lowercase

mystr = "Hey guys this is Karan from Python Pool"
print(mystr.lower())

Output:

hey guys this is karan from python pool
Python Basic Program to Convert String to Lowercase

The above example showing the output of the lower() function of Python. The output contains no camel case characters in the given string after conversion. Here, before the conversion, the last string word contains all letters in a capital case. The Python also converts these letters to the small letter using lower().

Example 2: Program to Convert User Defined Input String to Lowercase

#lower() with user defined input()
 
str_input = input("Enter string whose lowercase you want:  ")
print ("The original string you Entered:" ,str_input)
print ("String with lower() = ", str_input.lower())

Output:

Enter string whose lowercase you want:  Python pool is Best Place to Learn Python 007
The original string you Entered: Python pool is Best Place to Learn Python 007
String with lower() =  python pool is best place to learn python 007

Here in the above program, we take the input from the user using the input() function. Then with the help of lower() method, we converted the user-defined string to the lower case.

Points to Be Noted:

  1. lower() does not take any arguments, Therefore, It returns an error if a parameter is passed.
  2. Digits and symbols return are returned as it is, Only an uppercase letter is returned after converting to lowercase.

Python Program to Convert String to Lowercase Using casefold()

str.lower() converts the string to lowercase, but it doesn’t convert the case distinctions in the string.

For example, ß in German is equal to double s – ss, and ß itself is already lowercase, therefore, str.lower() will not convert it.

But str.casefold() will convert ß to ss.

Syntax:

The syntax of the casefold() method is:

str.casefold()

casefold() Parameters

The casefold() method doesn’t take any parameters.

Return value from casefold()

The casefold() method returns the case folded string value.

Example: Python Lowercase Using casefold()

mystring = "PYTHON IS BesT! PYTHON IS beast"

# printing lowercase string using casefold
print("Lowercase string:", mystring.casefold())

Output:

Lowercase string: python is best! python is beast

When to use casefold()

So, if the purpose is to do case insensitive matching, you should use case-folding.

Here is why you should use casefold() for case insensitive matching and converting lowercase string in Python.

lowercase Python string using casefold

Python Program to Convert String to Lowercase Using ASCII Values

In this program, we are comparing the ASCII values to check there are any uppercase characters. If true, we are converting them to lowercase.

# Python Program to Convert String to Lowercase using ASCII Values
 
string = input("Please Enter your String : ")
string1 = ''

for i in string:
    if(ord(i) >= 65 and ord(i) <= 90):
        string1 = string1 + chr((ord(i) + 32))
    else:
        string1 = string1 + i
 
print("\nOriginal String in Uppercase  =  ", string)
print("The Given String in Lowercase =  ", string1)

Output:

Please Enter your String : Python is Best Language

Original String in Uppercase  =   Python is Best Language
The Given String in Lowercase =   python is best language

Unicode / ascii_val of lower case alphabets are from 97 to 122.
Unicode / ascii_val of upper case alphabets are from 65 to 90.
so the difference between lower case and uppercase alphabets is 32.

Python Program to Convert String to Lowercase Using Loop

In this Python Program, we will convert the string to lower case using loops. Here in this example, we are using the while loop. You can use the same strategy in for loop as well. You have to just replace while with for.

Example:

# Python Program to Convert String to Lowercase using while loop
 
string = input("Please Enter your Own String : ")
string1 = ''
i = 0

while(i < len(string)): if(string[i] >= 'A' and string[i] <= 'Z'):
        string1 = string1 + chr((ord(string[i]) + 32))
    else:
        string1 = string1 + string[i]
    i = i + 1
 
print("\nOriginal String in Uppercase  =  ", string)
print("The Given String in Lowercase =  ", string1)

Output:

Please Enter your String : Python is Best Language

Original String in Uppercase  =   Python is Best Language
The Given String in Lowercase =   python is best language

First, we used while loop to iterate characters in a String. Inside the While Loop, we used the If Else Statement to check the character is between A and Z or not. If true, we are adding 32 to its ASCII value. Otherwise, we are copying that character to string 1.

How to Lowercase First Letter of String in Python

For converting specific character in the string to lowercase you have to use an index value of string and lower() function. Additional use arithmetic operator to contact remain string.

See below example converting the first letter into a lowercase letter.

Example:

str = "WELCOME TO PYTHON POOL"
print(str[0].lower() + str[1:])

Output:

wELCOME TO PYTHON POOL

Must Read

How to Calculate Square Root in Python
Implementation of Stack in Python
Python User Input | Python Input () Function | Keyboard Input

Summary

In this tutorial of Python Lowercase, we learned to transform a given string to lowercase using string.lower() method, string.casefold() and others methods. We used well-detailed examples and all the possible ways.

Do comment if you have any doubt and suggestion on this tutorial.

Happy Coding!

Leave a Reply