Python program to convert a string into lowercase


Python has various built-in functions that can be used for capitalization of the string. In this article develop a python program to perform this task using its lower() function. The lower() method converts each uppercase character in the input string to lowercase before returning the lowercased strings. It returns the original string if the given string contains no uppercase letters. The lower function doesn’t take any parameters.

Let’s take an example to understand the conversion of string in lowercase −

  • SCHOOL ----------- school

  • UnIvErSiTy ----------- university

Syntax

ord()

The ord() is a predefined function used in Python to specify the unique code of a character.

lower()

The lower is an in-built function that can be used to convert a given string into lowercase.

Algorithm

The following steps are −

  • We are initializing a variable ‘str1’ which represents the string value in uppercase.

  • Then we store the empty value to the variable named ‘lo_str’ which will later get the string value in lowercase. [Example 1]

  • Then create the empty string lo_str that stores the built-in lower() with the given string to convert into lowercase. Next, print the result with the help of variable named lo_str.[Example 2]

  • Now start the for loop to iterate the string value into character ‘ch’.

  • Then store the character of the given string by using a predefined function i.e. ord(ch) to the variable ‘asc’ which means it represents the unique code of a specific character.

  • Moving ahead to use an if-else statement to check the condition of an ASCII value of uppercase.

“asc > 64 and asc < 91”

    If any character is found between 65-90 it represents an alphabetical character (A-Z).

  • There are two points to understanding the if-else statement −

    • lo_str = lo_str + chr(asc+32) − As we know the variable lo_str is empty and by adding the character ascii value with 32 will get the lowercase character of a string.

    • lo_str = str1 + chr(asc) − Here we simply add a string and the ASCII value of char into it which will not return the lowercase of a string.

  • Finally, we get the lowercase string value with the help of the variable ‘lo_str’.

Example 1

In this program, we will use the built-in method ord that generates the unique code of the character

str1= "TUTORIALSPOINT"
lo_str= ""
for ch in str1:
   asc = ord(ch)

   if asc > 64 and asc < 91:
      lo_str = lo_str + chr(asc+32)
   else:
      lo_str = str1 + chr(asc)
print("The lowercase string is: ",lo_str)

Output

The lowercase string is:  tutorialspoint

Example 2

In this program, we will store the input string to the variable ‘str’. Then store the conversion string i.e. ‘str1.lower()’ in the variable ‘lo_str’. Next, we are printing the result with the help of ‘lo_str’.

str1= "TuToRiAlSpOiNt"
lo_str = str1.lower()
print("The lowercase string is",lo_str)

Output

The lowercase string is tutorialspoint

Conclusion

We explored the concept of string conversion in lowercase. In the case of example 1, we use the concept of ASCII value and set the predefined method ord() to get the code of a unique character. Then using the if-else statement to get the result of the lowercase string. In the case of example 2, we simply use the predefined method i.e. lower(), and get the result.

Updated on: 01-Jun-2023

321 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements