7 Quick Ways to Check If String is Empty in Python

Introduction

Python strings are immutable, i.e., we can change the strings according to the operations performed. String Manipulation is the most important feature in python. You can manipulate the string by many methods such as String slicing techniques, looping over the elements, and string methods. But. Sometimes we come to the situation where we need to check if the Python string is empty or not and the list is empty. In this article, we will be discussing ways to check if the string is empty or not in Python.

NOTE : String which has spaces in them are empty string but has a non-zero size.

7 Different ways to check if the string is empty or not

Let us discuss certain ways through which we can check if the string is an empty string or not.

1. Using len() method

We will be calculating the length of the string with the help of len() in python. Then, we will check if the string’s length is equal to 0, then the string is empty; otherwise, not.

NOTE: If the string contains spaces in it the string is not treated as the empty string by using the len() function in python 

Let us look at the example for the better understanding of the concept:

#input empty with and without spaces string
s = ""
str = "    "

x = len(s)
y = len(str)

if x == 0:
    print("string is empty")
else:
    print("string is not empty")
    
if y == 0:
    print("string is empty")
else:
    print("string is not empty")

Output:

string is empty
string is not empty

Explanation:

Here, we have taken two input strings, s and str. s is an empty string, and str has whitespaces. Then, we have calculated the length of both the strings with the len() function in python. Then, we have applied the if-else loop and checked if the string’s length is equal to 0, then the print string is empty, and if it is not empty, print not empty. In the len(), whitespaces are not treated as empty in the string, giving the output as a not empty string. At last, we have printed the output of both the string.

2. Using not operator To Check If String Is Empty String In Python

Not operator is used to perform the same task as of len() function. The empty string is always equivalent to False in Python. If we want to check if the string is empty or not, we can check it with the help of not operator.

NOTE: If the string contains spaces in it the string is not treated as the empty string by using the not operator in python 

Let us look at the example for the better understanding of the concept:

#input empty with and without spaces string
s = ""
str = "    "

if not s:
    print("string is empty")
else:
    print("string is not empty")
    
if not str:
    print("string is empty")
else:
    print("string is not empty")

Output:

string is empty
string is not empty

Explanation:

Here, we have taken two input strings, s and str. s is an empty string, and str has whitespaces. Then, we have applied if else condition in which we have checked if not in string, then the string is empty. Otherwise, it is not empty. The not operator also does not treat whitespaces as the empty string so that the output will come as not an empty string of the second input string. At last, we have printed the output.

3. Using and operator + strip() function to check if string is empty string in python

Sometimes, we see that the whitespaces present in the string are not treated as empty strings. So, when we check for the empty string, we can also check for the whitespaces through the strip() function.

Let us look at the example for the better understanding of the concept:

#input empty with and without spaces string
s = ""
str = "    "

if s and s.strip():
    print("string is not empty")
else:
    print("string is empty")
    
if str and str.strip():
    print("string is not empty")
else:
    print("string is empty")

Output:

string is empty
string is empty

Explanation:

Here, we have taken two input strings, s and str. s is an empty string, and str has whitespaces. We have applied the if-else condition and checked with the strip() function that the string is empty or not. If the condition gets False, that means the string is empty, and else blocks get executed. In this function, whitespace is also treated as empty strings. At last, we have printed the output.

4. Using Strip() function only To Check Empty String In Python

Sometimes, we see that the whitespaces present in the string are not treated as empty strings. So, when we check for the empty string, we can also check for the whitespaces through the strip() function.

Let us look at the example for the better understanding of the concept:

#input empty with and without spaces string
s = ""
str = "    "

if s.strip():
    print("string is not empty")
else:
    print("string is empty")
    
if str.strip():
    print("string is not empty")
else:
    print("string is empty")

Output:

string is empty
string is empty

Explanation:

Here, we have taken two input strings, s and str. s is an empty string, and str has whitespaces. We have applied if else condition in which we strip the strings, i.e., whitespaces gets removed, and then if the string gets empty, it will be an empty string. Otherwise, the string is not empty. At last, we have printed the output.

5. Using not operator + str.isspace()

It works the same as the strip() function as it checks for the whitespaces in the strip. But, strip() function a lot of time as compared to str. isspace() function because strip() has to perform strip operation, which takes a lot of computation loads.

Let us look at the example for the better understanding of the concept:

#input empty with and without spaces string
s = ""
str = "    "

if s and not s.isspace():
    print("string is not empty")
else:
    print("string is empty")
    
if str and not str.isspace():
    print("string is not empty")
else:
    print("string is empty")

Output:

string is empty
string is empty

Explanation:

Here, we have taken two input strings, s and str. s is an empty string, and str has whitespaces. We have applied the if-else condition. In the if-else condition, we have applied the isspace() function, which will check all the string spaces. Finally, we have printed the output, and you can see the output that both the strings are empty.

6. Using __eq__ method

The dunder basically tells the methods that have double underscores before and after their names. We can check empty string through the __eq__ method also.

NOTE: If the string contains spaces in it the string is not treated as the empty string by using the __eq__ method in python 

Let us look at the example for the better understanding of the concept:

#input empty with and without spaces string
s = ""
str = "    "

if "".__eq__(s):
    print("string is empty")
else:
    print("string is not empty")
    
if "".__eq__(str):
    print("string is empty")
else:
    print("string is not empty")

Output:

string is empty
string is not empty

Explanation:

Here, we have taken two input strings, s and str. s is an empty string, and str has whitespaces. We have taken the __eq__ method. We have applied the given method in the if-else condition and checked if the string is empty or not. At last, we have printed the output. This method also doesn’t treat whitespaces as empty strings.

7. Using Regex To Check Empty String In Python

We can create a regex pattern to check if the given string is either empty or contains only white spaces.

Let us look at the example for the better understanding of the concept:

#import re library
import re
s = ""
str = "         "

if not s or re.search("^\s*$", s):
    print('String is empty or has spaces')
else:
    print('String is not empty')
if not s or re.search("^\s*$", s):
    print('String is empty or has spaces')
else:
    print('String is not empty')

Output:

String is empty or has spaces
String is empty or has spaces

Explanation:

Here, we have taken two input strings, s and str. s is an empty string, and str has whitespaces. We have imported the re library in python. Then, we have applied the if-else condition with the given format to check whether the string is empty or not. If the if-condition satisfies, then the output gets printed as it is an empty string. Otherwise, Output gets printed as not an empty string. Hence, you can see the output.

Also Read: 5 Ways to Check if a String is Integer in Python

Conclusion

In this tutorial, we have learned about the concept of checking the string is empty or not and if the string contains whitespaces only or not. We have explained the full concept in detail by taking all the ways to check if the string is empty. All the ways are explained in detail with the help of examples. You can use any of the methods you like to use according to your program or project requirement.

However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.

5 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments