To check an empty string in Python,
- Method 1: Using not operator
- Method 2: By using len() function
- Method 3: Using not + string.strip()
- Method 4: By using not + string.isspace()
Method 1: Checking empty string using not operator
The not operator in Python checks the string with just spaces to be non-empty, which should not practically be True.
strA = ""
# checking if string is empty
print("Check if the strA is empty : ", end="")
if(not strA):
print("The string is empty")
else:
print("No it is not empty")
Output
Check if the strA is empty : The string is empty
The string is empty, and one thing to note is that we did not even put the space in the string.
That is why it returns as empty. On the other hand, if we put a space in the string, it will not count as empty, and the not operator returns False.
strA = " "
# checking if string with space is empty
print("Check if the strA with space is empty : ", end="")
if(not strA):
print("The string is empty")
else:
print("No it is not empty")
Output
Check if the strA with space is empty : No it is not empty
Method 2: Using the len() function
To check an empty string in Python, use the len() function. If it returns 0, the string is empty; otherwise, it is not. If the string has something, it will count as a non-empty string; otherwise, it is an empty string.
strA = ""
# checking if string with space is empty
print("Check if the string is empty : ", end="")
if(len(strA)):
print("The string is not empty")
else:
print("The string is empty")
Output
Check if the string is empty : The string is empty
Here, if condition becomes False because it returns 0, that means the else condition becomes True, and the string is empty.
If the string contains whitespace, it is not an empty string.
strA = " "
# checking if string with space is empty
print("Check if the string is empty : ", end="")
if(len(strA)):
print("The string is not empty")
else:
print("The string is empty")
Output
Check if the string is empty : The string is not empty
Space in string counts as a character. That is why when you check for the empty, it returns False.
Method 3: By using len() + string.strip()
To check an empty string in Python, use the len() + string.strip() method. The string strip() method removes the whitespace from the string.
If it contains any space, the strip() function removes and checks if the string is empty or not using the len() function.
str = " "
# checking if string with space is empty
print("Check if the string is empty : ", end="")
if(len(str.strip())):
print("The string is not empty")
else:
print("The string is empty")
Output
Check if the string is empty : The string is empty
Here, no matter how many spaces you will add to the string, it strips all the spaces and checks the length of the string, and if it returns 0, that means the string is empty; otherwise, not.
str = " KRUNAL "
# checking if string with space is empty
print("Check if the string is empty : ", end="")
if(len(str.strip())):
print("The string is not empty")
else:
print("The string is empty")
Output
Check if the string is empty : The string is not empty
In this example, you can see that the string is not empty cause it has certain characters. So len() method returns the length of the string, and if the condition returns True.
Method 4: Using not + string.isspace()
The string.isspace() function checks if the string contains any space or not. If the string contains any space, then it returns True. Otherwise, it returns False.
We use the combination of string and not string.isspace() method to check whether the string is empty, regardless of the space.
This method is more efficient than the strip() method because it requires executing the strip operation, which takes computation loading time if the string contains many spaces.
str = " "
# checking if string with space is empty
print("Check if the string is empty : ", end="")
if(str and not str.isspace()):
print("The string is not empty")
else:
print("The string is empty")
Output
Check if the string is empty : The string is empty
Here, we are checking the negative condition of the isspace() function with an operator. If one of the conditions becomes False, then due to an “and operator” if condition returns False, the else condition will be executed.
This is the better way to check a pure empty string in Python.
That’s it.