Online Tutorials & Training Materials | STechies.com
Register Login

Python: Check if String is Empty

09 Apr 2020 10:40 am || 0

In this article, we will learn how to check whether a string is empty or not. A string in python is an immutable sequence of characters. Python strings are written within single or double inverted quote. To check if a string is an empty or not we have many built-in function and operators.

We can check if Python list is empty using:

Let us understand it more briefly with the help of an example.

Example 1: Using the not Operator

# Initializing an empty string
Str = ''
# Using not operator
if not Str:
    print ("String is empty")
else:
    print ("String is not empty")

Output

String is empty

Explanation

In the above example, we created an empty string ‘Str’. Then we used a not operator to reverse the false value.

In python, an empty string always evaluates to false. So when we passed an empty string to the if the condition it’ll be evaluated to false. But the not operator reverses the false value to true value.

Thus the if condition is set to true. And we got “String is empty” as an output.

Example 2: Using the len() Function

# Initializing an empty string
Str = ''
# Using len() function
Length_Str = len(Str)
# Using if-else Statement
if Length_Str == 0:
    print ("String is empty")
else:
print ("String is not empty")

Output

String is empty

Explanation

In the above example, at first we initialized empty string ‘Str’. Then we used a built-in function len() to calculate the length of the string and stored it in the variable ‘Length_String ’. Then we used if statement to check if the length of the list is equals to zero or not.

If the condition sets to be true then the string is empty. Otherwise the string is not empty.   

Example 3: Comparing with Another Empty String

# Initializing an empty string ‘Str1’
Str1 = 'Hello'
# Initializing an empty string ‘Str2’
Str2 = ''
# Comparing both the string
if Str1 == Str2:
  print('The string is empty!')
else:
  print('The string is not empty!')

Output

The string is not empty!

Explanation

In the above example, we initialized two string Str1 and Str2. Suppose we have to check for a string ‘Str1’ if it is empty or not. Then we can do so by initializing an empty string say ‘Str2’. And then comparing ‘Str1’ with ‘Str2’ using the decision making statement i.e if-else condition. If ‘Str1’ is equal to ‘Str2’ then that means ‘Str1’ is an empty string. Else it is not empty. And this is how we can check for an empty string by comparing it with another empty string.

Note: In all the examples above, we only checked for string without blank spaces. But if a string only has a blank space then it makes the string non-empty. And the output we will get will be “String is not empty”. To counter this problem we have the following methods.

Example 4: Using strip() Method

# Initializing a string with blank space
Str = ' '
# Using strip() method
if  Str.strip():
    print ("String is not empty")
else:
print ("String is empty")

Output

String is empty

Explanation

In the above example, we used the strip() method to remove the blank space of our string ‘Str’. Then checked for if the string is empty or not. And we know, an empty string returns false value. Thus the statement in else block is printed.

If strip() method was not used. Then the string ‘Str’ would be considered as a blank string and not an empty string. And ‘String is not empty’ would be printed on the screen.

Difference between an empty string and a blank string. An empty string doesn’t include any characters or whitespace(tabs/ spacebar). Whereas a blank string has whitespaces.

Conclusion

In this article, we saw four different ways to check for an empty string. Including string with blankspaces/whitespaces. The four ways we discussed above are:

  • Using not
  • Using len()
  • Comparison with an empty string
  • Using strip()