How To Lowercase Python String with Example Programs

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

islower()

The islower() method returns True if all of the characters are in lowercase.

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

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

Advanced Ways to lowercase Python String

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

1. 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']

2. 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']

3. 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']

Working with Lowercase Strings: Best Practices

When handling lowercase strings in Python, adhering to best practices can improve your code’s efficiency and maintainability.

Here are some essential tips:

1. Use f-strings for String Formatting

F-strings (formatted string literals) are a concise and readable way to format strings in Python.

They allow you to embed expressions directly inside string literals, making code more elegant.

name = 'Alice'
age = 25
greeting = f"Hello, my name is {name} and I am {age} years old."
print(greeting)  # Output: 'Hello, my name is Alice and I am 25 years old.'

2. Avoid Using the + Operator for String Concatenation

While using the + operator is valid for concatenating strings, it can lead to performance issues when combining multiple strings. Instead, use f-strings or the join() method for more efficient concatenation.

# Inefficient approach
sentence = ''
for word in word_list:
    sentence += word + ' '

# Better approach
sentence = ' '.join(word_list)

3. Normalize and Validate Input

When dealing with user input or data from external sources, it’s essential to validate and normalize the lowercase strings.

This ensures consistency and prevents potential issues in the program.

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