Check If a String is Empty

A string can be checked if it's empty or not by following ways -

Simple Way

my_var = ""
if my_var == "":
    print("Variable is empty!")
else:
    print("Variable is not empty!")

Here we just compare the variable to an empty string.

Advanced Way

my_var = ""
if my_var:
    print("Variable is not empty!") # True when a non-empty string is found
else:
    print("Variable is empty!") # True when an empty string is found

Source: StackOverFlow

Last updated