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() # 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") |
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 # 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") |
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 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") |
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.
# 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") |
Output :
The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : Yes
Recommended Posts:
- String slicing in Python to check if a string can become empty by recursive deletion
- Python | Check whether a list is empty or not
- Python | Check if dictionary is empty
- Python | First Non-Empty String in list
- Python | Convert None to empty string
- Python program to check if a string is palindrome or not
- Python program to check if a given string is Keyword or not
- Python program to check if given string is pangram
- Python | Splitting list on empty string
- Python | Check if given string can be formed by concatenating string elements of list
- Python | Check if string ends with any string in given list
- Python | Check if a given string is binary string or not
- Python | Check for URL in a String
- Program to check if a string contains any special character
- Python | Check if given string is numeric or not
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.



