Skip to content

Latest commit

 

History

History
27 lines (20 loc) · 557 Bytes

File metadata and controls

27 lines (20 loc) · 557 Bytes

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