15
Check if string is empty or not in Python
ItsMyCode |
In this article, you will learn how to check if string is empty or not using different approaches with examples.
In Python, strings are an array of bytes representing the Unicode characters. Strings are immutable in Python, which means an immutable object is one that, once created, will not change in its lifetime.
There are various methods to check if a string is empty or not in Python. We will discuss the top 4 approaches in this article.
- Using len() method
- Using not operator
- Using not+str.strip() method
- Using not + str.isspace method
The len()
method is a built-in function in Python that returns the length of a string. We can use this technique to determine if the string is empty or not.
If the len()
method returns 0, it means the string is empty. Otherwise, it’s not empty.
Example – Check if the string is empty or not using the len() method.
The string with empty spaces is actually an empty string, but the len()
method treats whitespaces as Unicode characters and returns the length of the string.
In the below example, we determine if the string is empty or not by checking if the length of the string is equal to 0 using the “if” statement.
# Check if the string is empty or not using len() method
text1 = ""
text2 = " "
text3 = "Hello World"
print("Length of text1 :", len(text1))
print("Length of text2 :", len(text2))
print("Length of text3 :", len(text3))
if(len(text1) == 0):
print("String is empty")
else:
print("String is not empty")
if(len(text2) == 0):
print("String is empty")
else:
print("String is not empty")
if(len(text3) == 0):
print("String is empty")
else:
print("String is not empty")
Output
Length of text1 : 0
Length of text2 : 5
Length of text3 : 11
String is empty
String is not empty
String is not empty
The not operator can also perform similar to len()
method and check if the length of the string is 0 or not internally.
Again, the not operator considers the whitespaces a non-empty string similar to the len()
method, which is invalid.
Example – Check if the string is empty or not using not operator
# Check if the string is empty or not using not operator
text1 = ""
text2 = " "
text3 = "Hello World"
if(not text1):
print("String is empty")
else:
print("String is not empty")
if(not text2):
print("String is empty")
else:
print("String is not empty")
if(not text3):
print("String is empty")
else:
print("String is not empty")
Output
String is empty
String is not empty
String is not empty
In the above method, the strings with whitespaces are considered non-empty strings, and we can overcome this issue by using the strip()
method, which truncates the whitespaces at both leading and trailing ends.
In the below example, the strip()
method returns true if it encounters whitespaces, thus solving the issue.
# Check if the string is empty or not using not operator and strip() method
text1 = ""
text2 = " "
text3 = "Hello World"
if(not (text1 and text1.strip())):
print("String is empty")
else:
print("String is not empty")
if(not (text2 and text2.strip())):
print("String is empty")
else:
print("String is not empty")
if(not (text3 and text3.strip())):
print("String is empty")
else:
print("String is not empty")
Output
String is empty
String is empty
String is not empty
The most efficient way is to use a combination of not and str.isspace()
method because the strip()
method has to remove the whitespaces, and it’s a costly operation when compared to the issapce()
method.
# Check if the string is empty or not using not operator and isspace() method
text1 = ""
text2 = " "
text3 = "Hello World"
if(not (text1 and not text1.isspace())):
print("String is empty")
else:
print("String is not empty")
if(not (text2 and not text2.isspace())):
print("String is empty")
else:
print("String is not empty")
if(not (text3 and not text3.isspace())):
print("String is empty")
else:
print("String is not empty")
Output
String is empty
String is empty
String is not empty
The post Check if string is empty or not in Python appeared first on ItsMyCode.
15