Python | How to Check if String is Empty?

In Python, strings are immutable; they can manipulate and modify using different operations. The string values are initialized in Python using single or double quotation marks. Multiple methods are used for string manipulations, such as the string slicing method, finding/replacing/removing substring from the given string, and many others. An empty string in Python is any string that doesn’t contain any value or has a zero length.

This write-up will give you a detailed analysis of how to check whether the string is empty or not with multiple examples. The following Python contents are discussed in this guide:

So let’s begin!

How to Check if a String is Empty?

A Python program utilizes several inbuilt functions to check for the empty string. Some of the inbuilt functions include the “len()” function, “strip()”, “rstrip()” etc. In Python, the “len()” function and “not” operator count any whitespace inside the string as a character. Therefore, using these functions/operators, the strings with white spaces will not be considered empty strings. To overcome this problem, two functions can be combined to count the whitespace of the string as an empty string. Such as the “Not with strip() function” and “Not with isspace() function”.

How to install Kali Linux in Window...

Let’s see different methods one by one with their examples.

Method 1: How to Check if a String is Empty Using len()

In the below-mentioned code, the inbuilt “len()” function is used to check the empty string by finding its length.

Code-1: (String is initialized without space)

# initializing empty string
string_value1 = ""

print("First string is empty? : ", end="")
if(len(string_value1) == 0):
    print("Yes the string is empty")
else:
    print("No")

In the above code:

  • The string value is initialized without any space in a variable.
  • The “len()” function is initialized inside the parentheses of the “if” statement to calculate the length of the input string.
  • The if-else statement and the combination of the “len()” function is used to review if the string length is equal to zero; if yes, then the input string is empty. 

Output:

Based on the output, it can be verified that the string value is empty.

Code-2: (String is initialized with space)

# initializing empty space in string
string_value2 = " "
print("Second String is empty? : ", end="")
if(len(string_value2) == 0):
    print("Yes the string is empty")
else:
    print("No")

In the above code:

  • The string value is initialized with a white space.
  • The “len()” function gets the length of the given string, and here, the string length is not empty because the space value occupies some length in the given string.
  • The if-else statement, along with the combination of the “len()” function, is used to verify if the string has zero length. 

Output:

Based on the output, it can be verified that a string value initialized with white space is not an empty string.

Method 2: How to Check if a String is Empty Using Not Operator?

The ” not ” operator is used in the following code to determine whether the input string is empty or has a value. The “not” operator retrieves true when the actual condition is false. Here’s an example of code shown below to provide you a detailed understanding of this method:

Code:

# define empty string
string_value1 = ""

# checking if string is Empty or Not
print("First string is empty? : ", end="")
if(not string_value1):
    print("\nYes the string is empty")
else:
    print("No")

In the above code:

  • The empty string value is initialized inside the double quotation marks.
  • The “not” operator is used to verify whether the input string initialized in the code is empty or not.
  • The “not” operator executes the if block when the condition is false.
  • So, when there is no string value found in the given string, then the “not” operator runs the statement written inside the if condition.

Output:

It is verified from the output that the input string is empty.

Method 3: How to Check if a String is Empty Using Not Operator With Strip() Function?

In the given below example, the “not” operator is used along with the “strip()” function to check the empty string.

Code-1: (String is initialized without space)

# initializing empty string
string_value1 = ""

if not string_value1.strip():
    print("Empty string!")
else:
    print("Not Empty string!")

In the code mentioned above:

  • The string value is initialized without any space and stored in a variable.
  • The “not” operator is used along with the “strip()” function to check whether the string has any value or not.

Output:

The output is verified from the above snippet that the input string is empty.

Code-2: (String is initialized with spaces)

# initializing empty string
string_value1 = "  "

if not string_value1.strip():
    print("Empty string!")
else:
    print("Not Empty string!")

In the following code:

  • All the code is the same as the previous “code-1” example.
  • We are initializing empty strings with white spaces in this particular code.
  • The “strip()” function, along with the “not” operator, is used to check if the given string is purely empty or not.

Output:

The output verified that the given string is empty or blank.

Method 4: How to Check if a String is Empty Using Not Operator With isspace() Function?

In the following example, the “not” operator is used along with the “isspace()” function to check the empty string:

Code:

string_value1 = " "

if(string_value1 and not string_value1.isspace()):
    print("Input string is not empty")
else:
    print("Input string is empty")

In the above snippet:

  • An empty string containing whitespace is initialized in the program.
  • The combination of string value and “not” operator along with the “isspace()” function initialized inside the parentheses of the “if” statement. A string’s empty status can be determined using the “isspace()” function.
  • The “isspace()” function checks the whitespaces in the given string, similar to the previous “strip()” function.

Output:

Based on the output, it can be verified that the given string is empty.

Conclusion

In Python, the “len()” function, “not” operator, “not with strip()” and “not with isspace()” methods are used to check if the string is empty. The “len()” function and “not” operator count the whitespace as a character. Because of this, the len() function and not operator will not treat “strings with white spaces” as empty strings. To overcome this problem, two functions can be combined, such as the “Not” with “strip()” and “Not” with “isspace()”. This Python Guide Provided an in-depth review of how to check if the string is empty.