How to check if a string is None?

Asked

Viewed 217 times

1

I have the following code:

st = ""
if None == st:
   print True
else:
   print False

There is a form of the string st be equal to None in order to type the value on the screen True?

1 answer

4


Technically, there’s no way "a string can be None", because they are different things. String (more precisely str) is a class, while None is a constant built-in which represents the absence of value (and whose type is NoneType). They’re different types, which you can check so:

print(type('')) # <class 'str'>
print(type(None)) # <class 'NoneType'>

What is probably confusing you is the fact that in Python a variable can assume values of different types. For example, a variable can be a string and then it can change its value to a number:

s = 'abc'
s = 1

But that doesn’t mean the string 'abc' turned into the number 1. Actually it is the variable value s changed from a string to a int.

Therefore, there is no way to see "if a string is None". What you can check is whether the value of the variable is a string or None (or is one thing or another). Ex:

s = None
if s is None:
    print(True)
else:
    print(False)

The code above prints True, for the value of s is None. Although, if the goal is to print only True or False, just do print(s is None).

Now if you want to test if a string is empty, it looks like this:

s = ''
if s == '':
    print(True)
else:
    print(False)

Which also prints True (and which can also be simplified to print(s == '')).


There is still another possibility (but it depends a lot on what you want to do). In a context boolean, many values are considered True or False, in accordance with the rules of Truth Value Testing. Values such as None or the empty string, for example, are considered False, then you can at the same time test whether it is an empty string or None thus:

s = ''
if not s:
    print(True)
else:
    print(False)

s = None
if not s:
    print(True)
else:
    print(False)

The code above prints True twice, since both the empty string and None are values considered "false", so enter the if not s (remembering that in this case he will also enter the if if s is any of the "falsifiable" values described in documentation, as empty lists, etc).

Finally, the if above can also be shortened to print(not bool(s)) in both cases.


Another option is to check the variable type with isinstance. In this case, I’m just checking if the variable is a string (but not necessarily the empty string):

s = 'abc'
# é uma string, ou None
if isinstance(s, str) or s is None:
    print(True)
else:
    print(False)

The code above prints True if s is a string (any one, not just the empty string), or None (and can also be shortened to print(isinstance(s, str) or s is None)).


Anyway, it wasn’t clear what you want, but as you can see, there are several ways to test the value of a variable. Choose the one that makes the most sense for your case (but bearing in mind that it is not the string that can be None, yes the value of the variable).

  • I happen to have a "getsub" function and within this, there is a "for" cycle that at each iteration returns a sub-string of the genus, st[0:9], and at some point in the main function when it calls "getsub" it has an "if" that tests whether the sub-string that returns == None, and he writes the True value on the screen. But I still do not understand how this is possible. I have tested several possibilities and none returns None

Browser other questions tagged

You are not signed in. Login or sign up in order to post.