How to Check if a String is Empty in Python?

In this tutorial, I will explain how to check if a string is empty in Python. As a developer working on various projects, I came across a scenario where I needed to check whether a given string was empty. After researching and testing various methods I four efficient techniques to accomplish this task. Let us learn more about this topic today.

Check if a String is Empty in Python

Let us learn how to check if a string is empty in Python.

Read How to Print Strings and Variables in Python?

Method 1: Use the Equality Operator (==)

The most simple way to check if a string is empty is by using the equality operator (==) in Python. This method compares the string directly to an empty string ("").

city = ""

if city == "":
    print("The city name is empty.")
else:
    print("The city name is:", city)

Output:

The city name is empty.

You can look at the output in the screenshot below.

Check if a String is Empty in Python

In this example, if the city variable is an empty string, the output will be “The city name is empty.”

Check out How to Convert a String to a Float in Python?

Method 2: Use the len() Function

Another common method is to use the len() function in Python, which returns the length of the string. An empty string has a length of 0.

state = ""

if len(state) == 0:
    print("The state name is empty.")
else:
    print("The state name is:", state)

Output:

The state name is empty.

You can look at the output in the screenshot below.

How to Check if a String is Empty in Python

This approach is useful when you want to check the length of the string explicitly.

Read How to Remove Spaces from String in Python?

Method 3: Use the not Operator

The not operator is a Pythonic way to check for empty strings. It evaluates to True if the string is empty and False otherwise.

zipcode = ""

if not zipcode:
    print("The ZIP code is empty.")
else:
    print("The ZIP code is:", zipcode)

Output:

The ZIP code is empty.

You can look at the output in the screenshot below.

Check if a String is Empty in Python not Operator

This method is concise and often preferred for its readability.

Read How to Fix the “TypeError: string indices must be integers Error” in Python?

Method 4: Use the bool() Function

The bool() function in Python converts a string to a Boolean value. An empty string converts to False while a non-empty string converts to True.

street = ""

if not bool(street):
    print("The street name is empty.")
else:
    print("The street name is:", street)

This method is similar to using the not operator but makes the conversion to a Boolean value explicit.

Example: Validate User Input

Let’s consider a real-world scenario where we need to validate user input for a registration form. We want to ensure that the user has entered their city, state, and ZIP code. If any of these fields are empty, we will prompt the user to fill them in.

def validate_registration_form(city, state, zipcode):
    if not city:
        return "City name cannot be empty."
    if not state:
        return "State name cannot be empty."
    if not zipcode:
        return "ZIP code cannot be empty."

    return "Registration successful!"

# Example usage
user_city = "Los Angeles"
user_state = "California"
user_zipcode = ""

result = validate_registration_form(user_city, user_state, user_zipcode)
print(result)

In this example, the function validate_registration_form checks each input field and returns an appropriate message if any of them are empty.

Check out How to Pad Strings with Spaces in Python?

Conclusion

In this tutorial, I helped you to understand how to check if a string is empty in Python. I explained four methods such as using an equal operator(==), using the len() function, using the not operator, and using the bool() function. I also discussed a real-world example.

You may like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.