Best way to do a "not None" test in Python for a normal and Unicode empty string?

Question:

In Python 2.7, I’m writing a class that calls a function in an API which might, or might not, return an empty string. Furthermore, the empty string might be unicode u"", or non-unicode "". I was wondering what the best way to check for this?

The following code works great for an empty string, but not an empty unicode string:

class FooClass():
    string = ...
    string = might_return_normal_empty_string_or_unicode_empty_string(string)

    # Works for normal empty strings, not unicode:
    if string is not None:
        print "string is not an empty string."

Instead I’d have to write it like this to get it to work for unicode:

class BarClass():
    string = ...
    string = might_return_normal_empty_string_or_unicode_empty_string(string)

    # Works for unicode empty strings, not normal:
    if string is not u"":
        print "string is not an empty string."

…and like this to get it to work for both empty strings in non-unicode and unicode:

class FooBarClass():
    string = ...
    string = might_return_normal_empty_string_or_unicode_empty_string(string)

    # Works for both normal and unicode empty strings:
    if string is not u"" or None:
        print "string is not an empty string."

Is the third method the best way to do this, or is there a better way? I ask because writing a u"" feels a little too hard-coded to me. But if that’s the best way to do it, so be it. 🙂 Thanks for any help you can offer.

Asked By: Dave

||

Answers:

You never want to use is with anything that isn’t guaranteed to be a singleton. Check the length of the returned value, and if it’s an instance of unicode.

Check what ?

if s is None:
    print "s is None"
else:
    if isinstance(s, unicode):
        print "s is unicode, %r" % s
    else:
        print "s is bytes, %r" % s

    if s:
        print "s is not empty"
    else:
        print "s is empty"

Just check for None first, as usual.

Answered By: Jochen Ritzel

I have to challenge your first statement;

# Works for normal empty strings     <-- WRONG
if string is not None:
    print "string is not an empty string."

In Python 2.7.1, "" is not None evaluates to True – so string="" results in string is not an empty string (which it certainly is!).

Why bring None into it at all?

s = random_test_string()
s = API_call(s)

if len(s):
    # string is not empty
    pass
Answered By: Hugh Bothwell

Empty strings are considered false.

if string:
    # String is not empty.
else:
    # String is empty.
Answered By: Artur Gaspar

None is certainly a good thing to include in the conversation, especially if you’re writing a class that can be used by other programmers.

None is a valid, and wholly unique, value a string can have.

I’m new to Python so I’m not yet fully versed in the ‘correct‘ usage of None. My most frequent use of it is in initialization of strings and lists. If my code ever hits a case where I’m trying to do something with a string or list that my code has not specifically set to a value, I want to know about it. I’m using it as safety net of sorts.

if my_string is '': print('My string is NULL')
elif my_string is None : print('My string.... isn't')
else: print('My string is ' + my_string)
Answered By: Mike from PSG
# Python3.8
string = ' '
if string:
    print("Hello World from first statement")
    #this will be print the above line

if string and string.strip():
    print('This will not print anything')

string = None
if string:
    print("will not print anything")

string = ''
if string:
    print("still will not print anything")

first_string = ' '
second_string = ''
if first_string and second_string:
    print('will not print anything')
Answered By: maazu
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.