Sandeep Krishnan Sandeep Krishnan - 2 years ago 270
Python Question

How to check if a string is null in python

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

I need to check whether the

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

is the variable in which the value is stored.

Answer Source

Try this:

if cookie and not cookie.isspace():
    # the string is non-empty
    # the string is empty

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download