Python - How to check if string is Empty?


Python - Check if string is empty

To check if given string is empty, you can either use the string variable as a condition along with NOT logical operator, or compare the given string with an empty string "" using Equal-to comparison operator.

In this tutorial, we will write example programs for each of the processes mentioned above.

Examples

The first approach is to use the actual string value as a condition in the If-else statement.

The second approach is to use Equal-to comparison operator.

In the following examples, we cover these two approaches and check if the given string is empty or not.

1. Check if given string is empty using if else statement in Python

In this example, we will take an empty string in a variable my_string, and check if this string is empty or not, by using the string value as a condition in an If conditional statement.

If the string is empty, the string evaluates to a boolean value of False. So, if we use logical NOT operator before the string, we will get True if the string is empty, or False if the string is not empty.

Therefore, if the string is empty, if-block executes. But, if the string is not empty, else-block executes.

Python Program

my_string = ""

if not my_string:
    print("The string is empty.")
else:
    print("The string is not empty.")

Output

The string is empty.

Since the given string value is empty, if-block is executed.

Let us change the value in my_string variable to "Hello World", and run the program again.

Python Program

my_string = "Hello World"

if not my_string:
    print("The string is empty.")
else:
    print("The string is not empty.")

Output

The string is not empty.

Since the given string value is not empty, else-block is executed.

2. Check if given string is empty using Equal-to Comparison Operator in Python

In this example, we will take a string, and check if this string is empty or not using Equal-to comparison operator.

The syntax of the condition to check if the given string equals empty string is

my_string == ""

The Equal-to comparison operator compares the string value in my_string with the empty string "", and returns True if they are equal, or False if they are not equal.

We will use this expression in an if else statement.

Python Program

my_string = ""

if my_string == "":
    print("The string is empty.")
else:
    print("The string is not empty.")

Output

The string is empty.

Since the given string value is empty, if block is executed.

Let us change the value in my_string variable to "Hello World", and run the program again.

Python Program

my_string = "Hello World"

if my_string == "":
    print("The string is empty.")
else:
    print("The string is not empty.")

Output

The string is not empty.

Since the given string value is non-empty, else-block is executed.

Summary

This is how we check if a given string is empty or not. We have gone through two approaches where in the first approach we used the string value as a condition, and then in the second approach we used Equal-to comparison operator.