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.
# 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 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
Last Updated on November 8, 2021 by admin