Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (50.2k points)

This question already has an answer here:

A most elegant way to check if the string is empty in Python? 

I have a value cookie that is returned from a POST call using Python.

I need to check whether the cookie value is empty or null. Thus I need a function or expression for the if condition. How can I do this in Python? For example:

if cookie == NULL if cookie == None

P.S. cookie is the variable in which the value is stored.

1 Answer

0 votes
by (108k points)

Try this:

if cookie and not cookie.isspace(): 

# the string is non-empty 

else: 

# the string is empty

The above takes into consideration the cases where the string is None or a sequence of white spaces.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Feb 7, 2021 in Python by laddulakshana (16.4k points)

Browse Categories

...