Skip to content
Related Articles
Open in App
Not now

Related Articles

Python program to check if string is empty or not

Improve Article
Save Article
  • Last Updated : 24 Jan, 2023
Improve Article
Save Article

Python strings are immutable and hence have more complex handling when talking about its operations. Note that a string with spaces is actually an empty string but has a non-zero size. This article also discussed that problem and solution to it. 
Let’s see different methods of checking if string is empty or not.

Method #1 : Using len() 

Using len() is the most generic method to check for zero-length string. Even though it ignores the fact that a string with just spaces also should be practically considered as empty string even its non zero.

Python3




# Python3 code to demonstrate
# to check if string is empty
# using len()
 
# initializing string
test_str1 = ""
test_str2 = "  "
 
# checking if string is empty
print("The zero length string without spaces is empty ? : ", end="")
if(len(test_str1) == 0):
    print("Yes")
else:
    print("No")
 
# prints No
print("The zero length string with just spaces is empty ? : ", end="")
if(len(test_str2) == 0):
    print("Yes")
else:
    print("No")

Output

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No

Method #2 : Using not 

not operator can also perform the task similar to len(), and checks for 0 length string, but same as the above, it considers the string with just spaces also to be non-empty, which should not practically be true.  

Python3




# Python3 code to demonstrate
# to check if string is empty
# using not
 
# initializing string
test_str1 = ""
test_str2 = "  "
 
# checking if string is empty
print ("The zero length string without spaces is empty ? : ", end = "")
if(not test_str1):
    print ("Yes")
else :
    print ("No")
 
# prints No
print ("The zero length string with just spaces is empty ? : ", end = "")
if(not test_str2):
    print ("Yes")
else :
    print ("No")

Output

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No

Method #3 : Using not + str.strip() 

The problem of empty + zero length string can be possibly be removed by using strip(), strip() returns true if it encounters the spaces, hence checking for it can solve the problem of checking for a purely empty string. 
 

Python3




# Python3 code to demonstrate
# to check if string is empty
# using not + strip()
 
# initializing string
test_str1 = ""
test_str2 = "  "
 
# checking if string is empty
print ("The zero length string without spaces is empty ? : ", end = "")
if(not (test_str1 and test_str1.strip())):
    print ("Yes")
else :
    print ("No")
 
# prints Yes
print ("The zero length string with just spaces is empty ? : ", end = "")
if(not(test_str2 and test_str2.strip())):
    print ("Yes")
else :
    print ("No")

Output

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes

Method #4 : Using not + str.isspace 

Works in similar way as the above method, and checks for spaces in the string. This method is more efficient because, strip() requires to perform the strip operation also which takes computation loads, if no. of spaces are of good number.

Python3




# Python 3 code to demonstrate
# to check if string is empty
# using not + isspace()
 
# initializing string
test_str1 = ""
test_str2 = "  "
 
# checking if string is empty
print ("The zero length string without spaces is empty ? : ", end = "")
if(not (test_str1 and not test_str1.isspace())):
    print ("Yes")
else :
    print ("No")
 
# prints Yes
print ("The zero length string with just spaces is empty ? : ", end = "")
if(not (test_str2 and not test_str2.isspace())):
    print ("Yes")
else :
    print ("No")

Output

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes

Method: Using list comprehension 

Python3




string=""
x=["no" if len(string)>0 else "yes"]
print(x)

Output

['yes']

Method: Using Bool

One approach is using the bool function. The bool function returns False for empty strings and True for non-empty strings.

Here’s an example of using the bool function to check if a string is empty or not:

Python3




# Initializing a string
test_str = ""
 
# Checking if the string is empty
if not bool(test_str):
    print("The string is empty.")
else:
    print("The string is not empty.")
#This code is contributed by Edula Vinay Kumar Reddy

Output

The string is empty.

You can also use the bool function to check if a string is empty or not after removing any leading or trailing whitespaces using the strip method:

Python3




# Initializing a string
test_str = "  "
 
# Checking if the string is empty after removing leading and trailing whitespaces
if not bool(test_str.strip()):
    print("The string is empty.")
else:
    print("The string is not empty.")
#This code is contributed by Edula Vinay Kumar Reddy

Output

The string is empty.

Method: Using strip

Python3




#input empty with and without spaces string 
s = "" 
str = "    " 
    
if s.strip(): 
    print(f"string, string1 = '{s}', with no spaces is not empty"
else
    print(f"string, string1 = '{s}', with no spaces is empty"
        
if str.strip(): 
    print(f"string, string2 = '{str}', with spaces is not empty"
else
    print(f"string, string2 = '{str}', with spaces is empty"

Output

string, string1 = '', with no spaces is empty
string, string2 = '    ', with spaces is empty

Method: Using __eq__ Method

Python3




#taking an empty string and a string with spaces only 
string1 = "" 
string2 = "   " 
    
if "".__eq__(string1): 
    print(f"string, string1 = '{string1}', with no spaces is empty"
else
    print(f"string, string1 = '{string1}', with no spaces is not empty"
        
if "".__eq__(string2): 
    print(f"string, string1 = '{string2}', with no spaces is empty"
else
    print(f"string, string1 = '{string2}', with no spaces is not empty"

Output

string, string1 = '', with no spaces is empty
string, string1 = '   ', with no spaces is not empty

Method: Using “and” Operator + strip() Function

Python3




#input empty with and without spaces string 
string1 = "" 
string2 = "    " 
    
if string1 and string1.strip(): 
    print(f"string, string1 = '{string1}', with no spaces is not empty"
else
    print(f"string, string1 = '{string1}', with no spaces is empty"
        
if string2 and string2.strip(): 
    print(f"string, string2 = '{string2}', with spaces is not empty"
else
    print(f"string, string2 = '{string2}', with spaces is empty"

Output

string, string1 = '', with no spaces is empty
string, string2 = '    ', with spaces is empty

The “bool” approach for checking if a string is empty or not has a time complexity of O(1), since it simply checks the truth value of the string, which is a constant time operation. The Auxiliary space is also O(1) since it only requires a single boolean variable to store the truth value of the string.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!