Lowercase Python String with Example Programs

In this article, you will learn about the lowercase Python string function. These methods are built-in and are used to handle strings. This article also gives advanced examples to help you learn fast and easily.

In Python, the lower() function returns a string with all lowercase letters. The symbols and digits are disregarded.

What is lower() method in python?

The lower() method changes all the capital letters in a string into lowercase letters and returns the converted string.

Syntax:

string.lower()

For example:

message = 'PYTHON IS GOOD FOR OPENCV'

# convert message to lowercase

print(message.lower())

Output:

python is good for opencv

lower()

The lower() is a Python built-in string-handling method. The lower() method takes a string and returns it with all letters in lowercase. It changes all uppercase letters to lowercase. If no uppercase characters exist, the original string is returned.

For example:

Input : string = 'PYTHONFORFREE'

Output:

pythonforfree

Exceptions and Errors

  • There are no counterarguments. Therefore, if an argument is passed, it returns an error.
  • Only an uppercase letter is returned after the characters have been changed to lowercase. Digits and symbols are returned exactly as they were.

For example:

# Python code for implementation of lower()

# Checking for lowercase characters

string = 'PYTHONFORFREE'

print(string.lower())

string = 'PythonForFree'

print(string.lower())

Output:

pythonforfree

pythonforfree

For example:

# first string

StringTwo = "PYTHON IS FOR OPENCV"

# second string

StringTwo = "Python Is For OpenCV!"

if(StringOne.lower() == StringTwo.lower()):

  print("The strings are same.")

else:

  print("The strings are not same.")

Output:

The strings are same.

islower()

The islower() method returns True if all of the characters are in lower case. Otherwise, it returns False. Only the letters of the alphabet are checked, not the numbers, symbols, or spaces.

This function determines whether or not the argument contains any lowercase letters.

For example:

hdkaagdgalautwpfwjrwwrfksfjsgnkwgf

Syntax:

string.islower()

Exceptions and errors

  • It returns “True” for whitespace, but if there is only whitespace in the string, it returns “False.”
  • It does not accept any parameters, thus, if a parameter is supplied, it will return an error.
  • If the string solely contains digits and symbols, it returns “True”; otherwise, it returns “False.”

For example:

# Python code for implementation of isupper()

# checking for lowercase characters

string = 'pythonforfree'

print(string.islower())

string = 'PythonForFree'

print(string.islower())

Output:

True

False

Python Lowercase List

  • str.lower() function and for loop
  • map() function
  • List comprehension method

Using the str.lower() function and a for loop

The str.lower() method converts all uppercase characters in a string into lowercase characters and returns the result, which can be used in machine learning.

In addition to the str.lower() function, the for loop is also used to go through each string in a given list and data types.

For example:

company = ["ITsourceCode","SourCEcodeHerO","pROudpiNoy"]

for i in range(len(company)):

    company[i] = company[i].lower()

print(company)

Output:

['itsourcecode', 'sourcecodehero', 'proudpinoy']

Using map() function

With Python’s map() function, you can run a certain process on each of the items in an iterable. The result of calling this function is an iterator.

A lambda function is a small, anonymous function that takes any number of arguments and only has one expression. The lambda function will also be used in this method, along with the map function and data science. 

For example:

company = ["ITsourceCode","SourCEcodeHerO","pROudpiNoy"]

convert = (map(lambda x: x.lower(), company))

toLower = list(convert)

print(toLower)

Output:

['itsourcecode', 'sourcecodehero', 'proudpinoy']

Using list comprehension method

List comprehension is a much faster way to make new lists from the items in an existing list. This method makes a new list where all the items are written and returns lowercase.

For example:

company = ["ITsourceCode","SourCEcodeHerO","pROudpiNoy"]

toLower = [x.lower() for x in company]

print(toLower)

Output:

'itsourcecode', 'sourcecodehero', 'proudpinoy']

Upper()

upper() is a built-in method in Python that is used to handle string methods. The upper() method takes a string and turns it into all capital letters. It changes all uppercase characters to lowercase letters. If there are no lowercase characters, the original string is returned.

Syntax:

string.upper()

For example:

# Python code for implementation of upper()

# checking for uppercase characters

string = 'pythonforfree'

print(string.upper())

string = 'Hello, I'm Glenn Magada Azuelo'

print(string.upper())

Output:

PYTHONFORFREE

HELLO, I'M GLENN MAGADA AZUELO

isupper()

In Python, isupper() is a built-in method for working with strings. This method returns true if all the letters in the string are uppercase; otherwise, it returns false.

Syntax:

string.isupper()

For example:

# Python code for implementation of isupper()

# checking for uppercase characters

toUpper = 'PYTHONFORFREE'

print(string.isupper())

toUpper = 'PythonForFree'

print(string.isupper())

Output:

True

False

Advanced Example of lowercase and uppercase program

The goal is to develop a Python program that, given a string, counts uppercase, lowercase, and spaces and changes between them. This application provides a safe browsing experience (converting lowercase to uppercase and vice versa).

For example:

string ='Glenn Magada Azuelo is A writer and computer programmer'

newstring =''

count1 = 0

count2 = 0

count3 = 0



for a in string:

	if (a.isupper()) == True:

		count1+= 1

		newstring+=(a.lower())

		

	elif (a.islower()) == True:

		count2+= 1

		newstring+=(a.upper())

	elif (a.isspace()) == True:

		count3+= 1

		newstring+= a

		

print("In original String : ")

print("Uppercase -", count1)

print("Lowercase -", count2)

print("Spaces -", count3)



print("After changing cases:")

print(newstring)

Output:

In original String : 

Uppercase - 4

Lowercase - 43

Spaces - 8

After changing cases:

gLENN mAGADA aZUELO IS a WRITER AND COMPUTER PROGRAMMER

Conclusion

I hope this lesson has helped you learn a lot. Check out my previous and latest articles for more life-changing tutorials that could help you a lot.

Leave a Comment