How do I check if a string is empty in Python?


Does Python have something like an empty string so you can do the following?

if mi_cadena == string.empty:

Regardless, what's the most elegant way to check if a string is empty in Python? I find it weird to check each time if the string is exactly "" and I feel like there must be some easier way.


Empty strings are "falsies" , meaning they are considered false in a boolean context, so you can just say:

if not mi_cadena:

This is the preferred way, if you know your variable is a string. It is actually recommended by PEP 8 , in the “Programming Recommendations” section :

For sequences, (strings, lists, tuples), use the fact that empty sequences are false.

If your variable can be of some other type, then you should use mi_cadena == "" . See the Evaluate to True/False documentation for other values ​​that are false in boolean contexts.

For very specific cases it may be preferable and safer to use: if "".__eq__(myString): . Check the link for the detailed explanation.

Scroll to Top