Python program to check if string is empty or not

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# to check if string is empty
# using len()
  
# inilializing 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")

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# to check if string is empty
# using not
  
# inilializing 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")

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# to check if string is empty
# using not + strip()
  
# inilializing 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")

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 code to demonstrate 
# to check if string is empty
# using not + isspace()
  
# inilializing 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")

chevron_right


Output :

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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.