Home » How to Check if a String is Empty in Python

How to Check if a String is Empty in Python

by Online Tutorials Library

How to Check if a String is Empty in Python

Python strings remain immutable, which means we can change them based on the operations we execute. Python’s most significant functionality is string manipulation. Many methods, including string slicing approaches, looping through its elements, and string methods, can be used to alter the string. It’s vital to understand that a string with spaces is effectively a non-zero length empty string. That issue will be discussed in this tutorial, as well as a possible remedy. As a result, when we use the len() or “not” operator to check for an empty string, it actually counts space as nothing less than a character of the string, therefore a string with space will not be counted as an empty string.

In Python, we can use one of the basic ways described below to check for an empty string.

  1. Using not operator
  2. Using len() function
  3. Using not + string.isspace()
  4. Using len() + string.strip()
  5. Using and + string.strip()
  6. Using __eq__

Using not Operator

The not operator performs the same job as the len() function. In Python, an empty string is indeed equal to False. The not operator can be used to determine whether a string is indeed empty or not. The not operation in Python prevents the string from being interpreted as an empty string if it contains spaces.

Example

We’ve used two type strings, string1 and string2, in this example. The string2 has whitespaces, and string1 is an empty string. Then we used an “if else” condition to check if the given string is empty if it is not in the string. It is not, however, empty. Whitespaces are not treated as empty strings by the not operator, hence the output will not be an empty string for the case of second input string. Finally, the result has been printed.

Code:

Output:

string, string1 = '', with no spaces is empty  string, string2 = '   ', with spaces is not empty  

Using len() Method

We’ll use python’s len() function to determine the length of the string. Then, if the length of the string equals 0, the string is blank; otherwise, it is not. When using the len() technique in Python, the string isn’t really considered as an empty string if it contains spaces.

Example

We’ve used two type strings, string1 and string2, in this example. The string2 has whitespaces, and string1 is an empty string. The length of each strings was then computed using Python’s len() function. Then we used the “if-else” loop to check if the string’s length is equal to zero, in which case the condition prints that the string is empty, and in which case the print string is not empty. Whitespaces in the string are not considered as empty by len(), resulting in a not empty string. Finally, we printed the result of both strings.

Code:

Output:

string, string1 = '', with no spaces is empty  string, string2 = '   ', with spaces is not empty  

Using not Operator + str.isspace()

It checks for whitespaces there in strip in the same way that the strip() function does. However, strip() function takes a long time when opposed to str. isspace() since strip() must execute a strip operation, which requires a lot of computing work.

Example

We’ve used two type strings, string1 and string2, in this example. The string2 has whitespaces, and string1 is an empty string. The “if-else” condition has been used. We used the isspace() method in the if else condition, which checks for all string spaces. Finally, the output has been printed, and you can see that both strings are empty.

Code:

Output:

string, string1 = '', with no spaces is not empty  string, string2 = '   ', with spaces is not empty  

Using len() + string.strip()

In Python, use the len() + string.strip() technique to verify for a completely empty string. The whitespace in a string is removed using the string.strip() method. If there is any space in the string, the strip() method removes it, and the len() function checks whether the string is empty or not.

Example

Regardless of how many spaces we put in the string, it strips them all out and verifies the length of the string; if it returns 0, the string is empty; else, it isn’t.

Code:

Output:

string, string1 = '', with no spaces is empty  string, string2 = '   ', with spaces is empty  

Using “and” Operator + strip() Function

We’ve noticed that whitespaces in strings aren’t always interpreted as empty strings. So, when we use the strip() function to verify if the string is empty, we can indeed check for whitespaces too.

Example

We’ve used two input strings, string1 and string2, in this example. The string2 has whitespaces, and string1 is an empty string. We then used if-else condition and the strip() function to see if the string was empty or not. If the condition is False, the string is empty, and blocks are executed otherwise. Whitespace is also considered as an empty string in this method. Finally, the result has been printed.

Code:

Output:

string, string1 = '', with no spaces is empty  string, string2 = '   ', with spaces is empty  

Using Strip() Function

We’ve noticed that whitespaces in strings aren’t always interpreted as empty strings. So, when we use the strip() function to verify if the string is empty, we can indeed check for whitespaces too.

Example

We’ve used two input strings, string1 and string2, in this example. The string1 has whitespaces, and string2 is an empty string. We’ve used an if else condition to strip the strings, removing whitespaces, and then returning an empty string if the string becomes empty. The string is not empty otherwise. Finally, the result has been printed.

Code:

Output:

string, string1 = '', with no spaces is empty  string, string2 = '   ', with spaces is empty  

Using __eq__ Method

The dunder identifies methods with two underscores before as well as after their names. The __eq__ method can also be used to check for an empty string. When calling the __eq__ technique in Python, the string is not considered as an empty string if it contains spaces.

Example

We’ve used two input strings, string1 and string2, in this example. The string2 has whitespaces, and string1 is an empty string. The __eq__ function has been used. In the if-else condition, we used the given method to verify if the string is blank or not. Finally, the result has been printed. Whitespaces are not treated as empty strings in this approach.

Code:

Output:

string, string1 = '', with no spaces is empty  string, string1 = '   ', with no spaces is not empty  

You may also like