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:
- The len() function
- The len() and strip() method
- The not operator
- not and strip() method
- not and isspace() method
Let’s see each method with examples.
Check for empty strings using len() function
len() function returns the number of items (length) in a string in python. If the string is empty it will return us
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
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
Using len() and strip() to check for empty string
strip() remove the leading and trailing spaces or whitespaces from the given string.
str2 = ' ' if (len(str2.strip()) == 0): print('String is empty') else: print('the string is not empty')
String is empty
Here, first, we have removed the spaces from str2 using
strip() function and then checked it’s length using the
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.
not operator to check for empty strings in Python
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
x = 1 print(not x==3) #result: True print(not x==1) #result: False
The string is evaluated to
False in a Boolean context, if the given string is empty or not.
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
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
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
not operator with
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 will remove the spaces from the string, and then we can use the
not operator on it with the
if else statement.
str2 = ' ' if (not str2.strip()): print('String is empty') else: print('The string is not empty')
String is empty
not operator with the
isspace() method returns
True if the whole string is made of whitespaces else it returns
str1 = ' ' str2 = ' abc ' str3 = '' print(str1.isspace()) print(str2.isspace()) print(str3.isspace())
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
A zero-length string is also considered as
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.
str2 = ' ' if (str2 and not str2.isspace()): print('String is not empty') else: print('The string is empty')
The string is empty
Let’s understand the above expression (
str2 and not str2.isspace()) with a table:
|str2||not str2.isspace()||If() condtion||Result|
|‘ ‘||True||False||False||Empty string|
|‘ abc ‘||True||True||True||Not Empty|
isspace() method is better then
strip() method because it takes less computation power and is faster if dealing with strings with lots of whitespaces.
string.strip() method allocates a new string which is a waste whereas
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