Is empty string none in python?

The string data type in Python is immutable; that means you can not change a string once defined. A string with spaces is an empty string but has non-zero size. So, when you check the empty string with len() or not operator, it counts space as a character in the string, and hence it won’t count a string with space as an empty string.

The not operator in Python checks the string with just spaces to be non-empty, which should not practically be True.

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.

# app.py

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

Using len() function

To check an empty string in Python, use the len() function; if it returns 0, that means the string is empty; otherwise, it is not.

So, 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 else condition becomes True, and the string is empty.

If the string contains whitespace, it does not count as 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.

Using len() + string.strip()

To check a pure empty string in Python, use len() + string.strip() method. The string.strip() method removes the whitespace from the string. So 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.

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 if the string is empty or not 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 and operator. 

If one of the conditions becomes False, then due to an “and operator” if condition returns False, the condition will be executed.

This is the better way to check a pure empty string in Python.

That is it for this tutorial.

See also

How to trim whitespace from a string

Python String startswith()

Python String endswith()

Is empty string same as None in Python?

None is not the same as 0, False, or an empty string.

Is empty string equal to None?

An empty string is a String object with an assigned value, but its length is equal to zero. A null string has no value at all.

What is empty string in Python?

String len() It is valid to have a string of zero characters, written just as '' , called the "empty string". The length of the empty string is 0. The len() function in Python is omnipresent - it's used to retrieve the length of every data type, with string just a first example.

Does empty string evaluate to None in Python?

To check an empty string in Python, use the len() function; if it returns 0, that means the string is empty; otherwise, it is not. So, if the string has something, it will count as a non-empty string; otherwise, it is an empty string.