Skip to content

Python check empty string | Example code

  • by

The most elegant way would probably be to simply check an empty string if it’s true or falsy in Python.

Use if statement evaluated to False or True in a Boolean context if they are empty or not. They are not equal to False or True.

if not myString:

For example check an empty string in Python

Empty strings are “falsy” (python 2 or python 3 references), which means they are considered false in a Boolean context, so you can just do this:

my_string = ""

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

Output:

Python check empty string

Do comment if you have any doubts or suggestions on this Python string topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.