Python String Lower: How to Convert String to Lowercase

0
365
Python String lower() Method Example

To deal with uppercase and lowercase characters, Python provides many functions. We often need to convert a string from uppercase to lowercase and lowercase to uppercase.

How to convert string to lowercase

To convert a string to lowercase in Python, use the string.lower() method. The lower() method converts all uppercase characters of a string to lowercase characters and returns them.

Python string.lower()

The String lower() is a built-in Python method that returns a string where all characters are lowercase. The lower() method does not take any argument.

Syntax

string.lower()

Parameters

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

Return Value

The lower() method returns the lowercase string from the given string. It converts all uppercase characters to lowercase. If no uppercase characters exist, it returns an original string.

Example

To convert uppercase string to lowercase string, use the string.lower() method.

string = "EXPECTO PATRONUM"
print("Original String:", string)

lower_case = string.lower()
print("Lowercase String:", lower_case)

Output

Original String: EXPECTO PATRONUM
Lowercase String: expecto patronum

It is a straightforward example that needs no explanation.

How to check if a string is in lowercase

To check if a string is in lowercase in Python, use the string islower() method. The islower() method returns a boolean value, either True or False.

string = "Avada Kedavra"

print(string.islower())

Output

False

It returns False because the

String with alphanumeric characters

If you create a secure string, then the string may contain numeric characters and become a string with alphanumeric characters. After applying a lower() function to alphanumeric characters, see the below output.

string = "EXP3CT0 P7TR04UM"
print("Original String:", string)

lower_case = string.lower()
print("Lowercase String:", lower_case)

Output

Original String: EXP3CT0 P7TR04UM
Lowercase String: exp3ct0 p7tr04um

You can see that alphabetical characters are converted into lowercase, and numeric characters stay as it is. This is because the lower() function only works on alphabetical characters, not numbers. So digits and symbols return returned as it is.

Python lower() with if-else

The lower() function is often used to check some conditions in Python. To test conditions in Python, the if-else statement is used. Let’s see how we can use lower() and if-else statements.

string1 = "Avada Kedavra"
string2 = "aVaDa kEdAvRa"

if(string1.lower() == string2.lower()):
    print("The strings are same.")
else:
    print("The strings are different.")

Output

The strings are same.

Errors and exceptions

The string lower() method does not take any arguments; still, if you pass any arguments, it returns an error.

string1 = "Avada Kedavra"

string1.lower(1)

Output

TypeError: str.lower() takes no arguments (1 given)

We got the TypeError: str.lower() takes no arguments.

To get the length of the string, use a built-in len() method.

string = "Avada Kedavra"

print(len(string))

Output

13

Python string.count()

To count the number of characters that appear in the string in Python, use the string.count() method. The count() method accepts a character as an argument, which we need to count in the string. It returns the counter, which is a type integer.

string = "Avada Kedavra"

print(string.count("a"))

Output

4

We can also count entire words as sequences of characters using string.count() method.

string = "Avada Kedavra"

print(string.count("Avada"))

Output

1

Conclusion

Python lower() function converts uppercase characters of a string to lowercase characters. To convert lowercase characters to uppercase characters, use the Python upper() method.

That’s it.

Leave A Reply

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.