Python

How Do I Check If a String Is Empty in Python?

The strings are unchangeable. We cannot modify the string after defining it. Different operations are performed on strings. If we have a string that contains only whitespaces in it, that string is not considered empty. It contains the size of the non-zero value. So, if len() method and “not” operator is applied on this type of string, it considers whitespace as an element of the string. Sometimes in Python, we want to check whether the specified string is blank or not. To check the emptiness of the given string, use the “not” operator to utilize the variable of string instead of a condition, or utilize the equal operator to match an empty string. Now, we are going to explain numerous methods:

Use len() Method:

First, calculate the length of a string by using the len() function. If it is equal to 0, the string will be empty, else not. In Python, if the string has whitespaces in it, then the string is not considered as an empty string. For the execution of the code in Python, we install the “Spyder” software. In this code, we have two strings, space and string. The string space is empty. And the “string” contains spaces. On the next line, we use the len() function to calculate the length of these two strings:

After calculating the length, we make two variables, “x” and “y”, to store the value of the length. And if the length is not equal to 0, print “The string is not empty”. Similarly, if the length of the second string is equal to 0, it returns “The string is empty”. If not equal to 0, it returns “The string is not empty”. The len() function does not treat spaces as empty. Now, we have to run this code by pressing the “Run” option from the menu bar:

How Do I Check If a String Is Empty in Python

We have the output of both strings. For the first string, the length is equal to 0, so it returns with “The string is empty”. The second string length is not equal to 0, so it returns with “The string is not empty”.

Use “not” Operator:

In Python, if the defined string has space, utilize the “not” operator; it does not consider the string as a blank string. It carries out the same function, just like the len() function. In Python, the empty string corresponds to false. Here, we examine whether the defined string is blank or not by using the “not” operator:

In this instance, we take the strings named “space” and “string”. The first string, “space”, is blank. The “string” contains whitespaces. We apply the if-else condition. We perform the “not” operator on both the strings. This operator doesn’t consider spaces as blank strings:

The print statement of both the strings returns the value for both strings according to the condition applied by the “not” operator.

Use “and” operator and strip() Method:

In Python, sometimes, the spaces that exist in the strings may not be treated as blank strings. So, if we are looking for an empty string, we utilize the strip() method to look for spaces.

Here, we take the input strings. There are two strings, and these strings are presented by “space” and “string”. The “string” consists of whitespaces, and the “space” has no string.

The If-else condition is manipulated here. If the condition is not satisfied, it shows the string will be blank. Otherwise, the element will be executed. The strip() function treats the spaces as an empty string. We also utilize the “and” operator with the strip() method.

After applying the “and” operator and strip() function in both the strings, we get the output “The string is empty”.

Use “not” Operator and isspace() Method:

To check whether the string is empty or not, the isspace() function is utilized for this purpose. It works just like the strip() method because it looks for space in the string. However, the strip() method takes much more time than the isspace() method. This is because the strip() method carries out a strip operation. And this requires a lot of computational loads.

There are two strings in this example. The variables for these strings are “space” and “string”. The variable “string” contains space in it. And the “space” is an empty one. Now, we implement the if-else statement. The isspace() method is implemented in this if-else condition. So, we can take a look at all the whitespaces of strings. The isspace() method and “not” operator are implemented separately on the strings, “space” and “string”.

We have to get the output “The strings are empty” separately for both strings. This output is obtained by applying the “not” operator and isspace() function on both the strings.

Conclusion:

In this article, we discussed about the approaches of how to check whether the defined string is blank or not and if the string has only whitespaces. Here, we discuss the use of the len() function to calculate the length of two strings. After getting the lengths, we check whether the string is empty or not. We use the “not” operator to check the emptiness of the string. Similarly, we also utilize the “and” operator with the strip() method. In the end, we use the isspace() function with the “not” operator for this purpose. All these methods are discussed in detail by using different examples. We hope you found this article helpful. Please check Linux Hint for more informative articles.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content