How to check if a string is empty in Python

This tutorial will show you how to check if a string is empty in Python.

Updated March 29, 2023

Hello and welcome to this Python beginner’s tutorial on checking if a string is empty. Knowing how to check if a string is empty is essential in programming. This tutorial will show you how to check if a string is empty in Python.

What is an Empty String?

An empty string is a string with no characters in it. It is denoted by two double quotes ("") or two single quotes (''). An empty string has a length of 0.

Method 1: Using the len() Function

The len() function in Python returns the number of characters in a string. If a string is empty, the len() function will return 0. We can use this fact to check if a string is empty. Here’s an example:

# Using the len() function
string1 = ""
string2 = "Hello, world!"

if len(string1) == 0:
    print("string1 is empty")
else:
    print("string1 is not empty")

if len(string2) == 0:
    print("string2 is empty")
else:
    print("string2 is not empty")

In this code, we define two strings: string1 is an empty string, and string2 is a non-empty string. We then use the len() function to check if each string is empty. If the length of the string is 0, we print a message saying that the string is empty; otherwise, we print a message saying that the string is not empty.

Method 2: Using the not operator

In Python, the not operator is used to invert the truth value of a statement. We can use the not operator to check if a string is empty by checking if the string is not true. An empty string is considered false in Python. Here’s an example:

# Using the not operator
string1 = ""
string2 = "Hello, world!"

if not string1:
    print("string1 is empty")
else:
    print("string1 is not empty")

if not string2:
    print("string2 is empty")
else:
    print("string2 is not empty")

In this code, we define two strings: string1 is an empty string, and string2 is a non-empty string. We then use the not operator to check if each string is empty. If the string is considered false, we print a message saying that the string is empty; otherwise, we print a message saying that the string is not empty.

Method 3: Using the == operator

In Python, we can use the == operator to compare two strings. If two strings are equal, the == operator returns True; otherwise, it returns False. We can use this fact to check if a string is empty by comparing it to an empty string. Here’s an example:

# Using the == operator
string1 = ""
string2 = "Hello, world!"

if string1 == "":
    print("string1 is empty")
else:
    print("string1 is not empty")

if string2 == "":
    print("string2 is empty")
else:
    print("string2 is not empty")

In this code, we define two strings: string1 is an empty string, and string2 is a non-empty string. We then use the == operator to check if each string is equal to an empty string. If the string is equal to an empty string, we print a message saying that the string is empty; otherwise, we print a message saying that the string is not empty.

Method 4: Using IF Statement

We can also use the if statement to check if a string is empty. Here’s an example:

# Using if statement
string1 = ""
string2 = "Hello, world!"

if string1:
    print("string1 is not empty")
else:
    print("string1 is empty")

if string2:
    print("string2 is not empty")
else:
    print("string2 is empty")

In this code, we use the if statement to check if each string is empty. If the string is not empty, we print a message saying that the string is not empty; otherwise, we print a message saying that the string is empty.

Conclusion

In this tutorial, we have shown you four different methods to check if a string is empty in Python. We have covered using the len() function, the not operator, the == operator, and the if statement. It’s important to choose the right method that suits your needs and the context in which you’re working.

Keep practicing these methods and try to use them in your own projects. Remember that practice makes perfect, and the more you practice, the better you will get at Python programming. Thank you for reading, and happy coding!

Stay up to date on the latest in Coding Python with AI and Data Science

Intuit Mailchimp