0 votes
in Education by (1.7m points)
Does Python have something like an empty string variable where you can do:

if myString == string.empty:

Regardless, what's the most elegant way to check for empty string values? I find hard coding "" every time for checking an empty string not as good.

Select the correct answer from above options

1 Answer

0 votes
by (1.7m points)
Best answer
In Boolean context, sequences are evaluated either as FALSE or TRUE and empty strings are considered as false.

So you can simply check if the string is false using:

if not mystring:

You can also use the strip() method:

if myString.strip():

print("not an empty string")


print("empty string")

In case, the variable is something other than a string, use this:

 myString== “ “

If you wish to know what is python visit this python tutorial and python interview questions.