Python string not empty | Example code

Use not-operator expression in if statement to check string not empty in Python.

if not myString:

Python string not empty example

Simple example code.

my_str = ""
if not my_str:
    print("empty")
else:
    print("not empty")

Output:

Python string not empty

Another example with string

my_str = "Hello"
if not my_str:
    print("empty")
else:
    print("not empty")

Output: not empty

Do comment if you have any doubts and suggestions on this Python string code.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

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


Leave a Reply

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