Convert String to Lowercase in Python

You can convert string to lowercase in python by using lower(), swapcase(), and casefold() methods. The Python lower() method is a built-in function that converts all upper characters in a string to lowercase and returns the string in lowercase. If no uppercase characters exist, it returns the original string.

In this article, I will explain the different ways to convert string to lowercase in Python.

RelatedUse upper() to Convert String to Uppercase

1. Quick Examples of Convert String to Lowercase

If you are in a hurry, below are some quick examples of how to convert string to lowercase.


# Below are the quick examples

# Example 1: Convert string to lowercase
String = "WELCOME TO SPARKBYEXAMPLES"
print(String.lower())

# Example 2: Use lower() on literal
"WELCOME TO SPARKBYEXAMPLES".lower()

# Example 3: Convert alphanumeric to lowercase 
String = "WE5COM4 TO 5PARK8YEXAMPLE5"
print(String.lower())

# Example 4: Using swapcase() method
String = "WELCOME TO SPARKBYEXAMPLES"
print(String.swapcase())

# Example 5: Using casefold() method
String = "WELCOME TO SPARKBYEXAMPLES"
print(String.casefold())

2. Syntax of String lower() Method

Following is the syntax for creating lower() method. The function is called on the string object and returns a new string.


# Syntax of lower() function
string.lower()

2.1 Parameter of lower()

The lower() method does not take any parameters.

3. Convert String to Lowercase using lower()

You can use the lower() function to convert characters in a string to lowercase. The lower() function returns a new string with all the characters in the original string converted to lowercase.

Note that the lower() method does not modify the original string instead it returns a new string with the lowercase version of the original string. If you want to modify the original string, you can assign the result of the lower() method back to the original string: For example,


# Convert string to lowercase
String = "WELCOME TO SPARKBYEXAMPLES"
print(String.lower())

Yields below output.


# Output:
# welcome to sparkbyexamples

You can also convert string with alphanumeric characters (containing both letters and numbers) to lowercase.


# Convert alphanumeric string to lowercase
String = "WE5COM4 TO 5PARK8YEXAMPLE5"
print(String.lower())

# Output
# we5com4 to 5park8yexample5

4. Convert Original String to Lower Case

Note that the lower() method does not modify the original string instead it returns a new string with the lowercase version of the original string. If you want to modify the original string, you can assign the result of the lower() method back to the original string.


# Convert alphanumeric string to lowercase
StringObj = "WELCOME TO SPARKBYEXAMPLES"
StringObj = StringObj.lower())
print(StringObj)

# Output:
# welcome to sparkbyexamples

5. Convert String to Lowercase using swapcase()

You can also use the swapcase() function to convert string to lowercase in python.


# Convert string to lowercase using swapcase() method
String = "WELCOME TO SPARKBYEXAMPLES"
print(String.swapcase())

# Output
# welcome to sparkbyexamples

6. Convert String to Lowercase Using casefold() Method

Similarly, you can also convert uppercase to lowercase in python using casefold() function.


# Convert string to lowercase using casefold() method
String = "WELCOME TO SPARKBYEXAMPLES"
print(String.casefold())

# Output
# welcome to sparkbyexamples

7. One Use Case of Converting to Lower Case

One real-time use case of converting a string to a lowercase would be to compare two strings by ignoring the case. The below example converts both strings to lowercase before the comparison.


# Comparison of strings using lower() method
String = "WELCOME TO SPARKBYEXAMPLES"
String1 ="WeLComE TO SpArKbYExAmPlEs"
 
if(String.lower() == String1.lower()):
    print("Strings are same")
else:
    print("Strings are not same")

# Output
# Strings are same

Conclusion

In this article, I have explained different ways to convert all characters in a string to lowercase in Python. Fir, I covered using the lower() function and then explained casefold() & swapcase(). With these functions, If no uppercase characters exist in your string, it returns the original string.

Happy Learning !!

Leave a Reply

You are currently viewing Convert String to Lowercase in Python