3 Ways To Lowercase A String In Python

Introduction 

In most cases, we need to change the case of our string, And not one method fits each and every condition. In this blog, I'm going to explain three ways to convert a string in lowercase with Python. It depends on your requirements which method is suitable to fulfill your condition. So let's look at these three methods.

  1. lower()
  2. casefold()
  3. swapcase()

lower()

It's an inbuilt method that converts the given string into lowercase. The syntax for this method is very easy "SoMe StRiNg".lower() and it will convert your string into lowercase. Let's have an example

str="It's a string With UPPERCASE And lowercase"
print("Normal string ")
print("......................................")
print(str)
print(".......................................")
print("Lowercase  string using lower() method")
print(".......................................")
print(str.lower())
print(".......................................")

Output

Lowercase A String In Python
Figure - Lowercase with lower()

casefold()

This one is also an inbuilt method provided by python. casefold() is more powerful than lower() if you want to make a comparison between two strings in python then I'll suggest this method because it is more powerful and provides more matches. The syntax for this method is "SoMe StRiNg".casefold()Let's have an example

str="ßE Positive"
print("Normal string ")
print("......................................")
print(str)
print(".......................................")
print("Lowercase  string using casefold() method")
print(".......................................")
print(str.casefold())
print(".......................................")

Output

Lowercase A String In Python
Figure - Lowercase with casefold()

swapcase()

By using swapcase() we can perform bidirectional case conversations if ur string contains lowercase only then it will convert them into uppercase. If your string contains uppercase only then it will convert them into lowercase and if your string contains both types of characters then it will swap these characters' cases if lower then upper if upper then lower. Let's have an example of these conditions.

str="lowercase"
print("Normal string ")
print("......................................")
print(str)
print(".......................................")
print(" using swapcase() method")
print(".......................................")
print(str.swapcase())
print(".......................................")
str2="UPPERCASE"
print("Normal string ")
print("......................................")
print(str2)
print(".......................................")
print(" using swapcase() method")
print(".......................................")
print(str2.swapcase())
print(".......................................")
str3="Mix Case"
print("Normal string ")
print("......................................")
print(str3)
print(".......................................")
print(" using swapcase() method")
print(".......................................")
print(str3.swapcase())
print(".......................................")

Output

Lowercase A String In Python
Figure - Case conversion with swapcase()

Conclusion

In this blog we have uses three different ways for case conversion, It all depends on your choice that which way fits better in your condition and requirement