Skip to content

Python string not empty | Example code

  • by

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.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 *