Python – Check if a string is Empty or Not

In this post, we will see different ways to check if a string is empty or not in Python.

In python, strings are immutable, which means we cannot make any changes to them once we have defined them. However, we can create a new string from the existing one in python.

What is an Empty String?

An empty string is a string with zero length, which means no characters, spaces, or white space.

If a string has only a space (' '), it will look like an empty string, but it is a non-zero length string. So if we use the len() function on it, it will be considered a non-empty string.

How to check for empty string in Python?

In Python, there are different ways to check if a string is empty using:

  1. The len() function
  2. The len() and strip() method
  3. The not operator
  4. not and strip() method
  5. not and isspace() method

Let’s see each method with examples.

Check for empty strings using len() function

The len() function returns the number of items (length) in a string in python. If the string is empty it will return us 0.

Example:

str1 = ''
str2 = ' '

print(len(str1))
# return: 0 (empty string)
print(len(str2))
# return: 1 (non-empty string)

As you can see, even though both the string (str1 and str2) looks empty, the str1 returns the length as 0 and is considered an empty string.

And in str2, the space(' ') is considered as a character and so we get the length as 1.

So how make the len() function ignore the spaces and whitespaces?

Well for that we have to trim those white spaces from the string first and then check the length of it using len().

Using len() and strip() to check for empty string

The strip() remove the leading and trailing spaces or whitespaces from the given string.

Example:

str2 = '  '

if (len(str2.strip()) == 0):
    print('String is empty')
else:
    print('the string is not empty')

Output:

String is empty

Here, first, we have removed the spaces from str2 using strip() function and then checked it’s length using the len() function.

Since all the spaces were trimmed by strip() so we get the length of the string as 0 (zero). And thus we get the “String is empty” in our console.

Using not operator to check for empty strings in Python

The not operator is a logical operator in Python, that will return True, if the expression is False. and False if the expression is True.

Learn more about not operator here

Example:

x = 1
print(not x==3)
#result: True

print(not x==1)
#result: False

The string is evaluated to True or False in a Boolean context, if the given string is empty or not.

The not operator is used with the if statement in a program.

We can use the not operator along with the if statement to check if a string is empty or not in python

Example:

str1 = ''
str2 = ' '

#str1 - with no space in the string
if (not str1):
    print('String is empty')
else:
    print('The string is not empty')

#result: String is empty

#str2 - with space in the string
if (not str2):
    print('String is empty')
else:
    print('The string is not empty')

#result: String is not empty

The str1 have no spaces in it, so the string is considered an empty string. And on the other hand str2 with the spaces inside it, returned False, and it is considered as not empty.

Now since the string with spaces is not considered an empty string, the workaround to it would be using the strip() or the isspace() function.

Using not operator with strip() method

Since we don’t get the correct results if the string contains spaces or whitespaces, we have to use the not operator along with the strip() method.

The strip() method will remove the spaces from the string, and then we can use the not operator on it with the if else statement.

Example:

str2 = '  '

if (not str2.strip()):
    print('String is empty')
else:
    print('The string is not empty')

Output:

String is empty

Using not operator with the isspace() method.

The isspace() method returns True if the whole string is made of whitespaces else it returns False.

Example:

str1 = '   '
str2 = ' abc '
str3 = ''

print(str1.isspace())
print(str2.isspace())
print(str3.isspace())

Output:

True
False
False

As you can see, it shows True only if the whole string is made of whitespace. If the string contains whitespaces along with other characters, then it will return False.

A zero-length string is also considered as False in isspace() method. i.e "".isspace() = False

Now we can use the not operator with the isspace() method to check if a string is empty in Python.

Example:

str2 = '  '

if (str2 and not str2.isspace()):
    print('String is not empty')
else:
    print('The string is empty')

Output:

The string is empty

Let’s understand the above expression (str2 and not str2.isspace()) with a table:

 str2not str2.isspace()If() condtionResult
‘      ‘TrueFalseFalseEmpty string
FalseTrueFalseEmpty String
‘  abc  ‘TrueTrueTrueNot Empty

The isspace() method is better then strip() method because it takes less computation power and is faster if dealing with strings with lots of whitespaces.

And the string.strip() method allocates a new string which is a waste whereas string.isspace() doesn’t.

Conclusion: In this post, we have learned what is an empty string, how to use the not operator and len() method to check for empty strings in Python and how we can solve the issues or errors we get with non-zero length strings made with whitespaces using strip() and isspace() method.


Related Article:

Split String and get the first element using python

How to split a list into multiple lists using Python

Python – Remove Newline From Strings (3 Ways)

How to Append String in Python

Convert a string into an integer or float using Python

Split string by whitespace in Python

Solving TypeError: string indices must be integers error in Python

Replace character in String by index in Python

Scroll to Top