You could cut down on code slightly by just writing

if variable and variable.upper() == "X":
    #Do something

If the variable is none or empty, then it's equivalent to False.

Answer from obmarg on Stack Overflow
Top answer
1 of 1
5

There's a conceptual difference between

  • an empty value
  • a default value
  • the absence of a value

Python's None object is generally used to indicate the absence of a value, and is similar to a null pointer in other languages. This isn't 100% perfect, but it's a good fit most of the time.

You can then check whether you have a value if something is not None: .... It is a common error to check a possibly-empty value for truthiness as in if something: ..., because a present value could also be falsey. Consider:

                   value = True    value = False   value = None
bool(value)        True            False           False
value is not None  True            True            False
                                   ^^^^^^^^^^^^^

So a simple truthiness check would not find present values such as False, 0, ''. Furthermore, many empty values are true-ish, especially most user-defined objects. User-defined objects can override truthiness checks via the __bool__ dunder-method, adding to the possible confusion. The something is None/something is not None check suffers from no such problems because it checks for object identity.

Python function arguments can take a default value. However, this default is evaluated at function definition time, which makes these defaults unsuitable for expensive objects or mutable objects. Then, setting the default to None and supplying the default within the function can be better:

def append_items(items=None):
  """Append some items to the list, defaulting to a new list."""
  if items is None:
    items = []

  items.append(1)
  items.append(2)
  return items

As an added benefit, callers can now explicitly request the default value, without having to know exactly what it is.

In some scenarios this can be problematic: when None is an allowed value! Then, you can create your own singleton that stands in for the default value. I sometimes write code like this:

_default = []  # some private object that has an identity

def my_function(argument=_default):
  if argument is _default:
    argument = "the default was chosen"
  return str(argument)

assert my_function() == "the default was chosen", "Argument is optional"
assert my_function(123) == "123", "Can take values"
assert my_function(None) == "None", "None is not the default value"
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-empty-string-to-none-conversion
Empty String to None Conversion - Python - GeeksforGeeks
July 12, 2025 - Since s is an empty string the result is None. or operator in Python returns first truthy value it encounters or last value if none are truthy.
๐ŸŒ
Quora
quora.com โ€บ What-is-the-difference-between-Null-and-empty-string-in-Python
What is the difference between Null and empty string in Python? - Quora
Answer (1 of 21): Null is a special ... in terms of Memory reference - because it has nothing and is nothing. Empty strings are like Containers....
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ python-how-to-check-if-a-value-is-none-or-empty-in-python-559594
How to Check If a Value Is None or Empty in Python | LabEx
It is often used to indicate that a variable has not been assigned a value or that a function does not return a value. ... Empty values, on the other hand, refer to data structures that contain no elements. For example, an empty string ("") ...
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ [deleted by user]
None or empty string - is one better than the other?
December 7, 2015 - Subreddit for posting questions and asking for general advice about all topics related to learning python. ... None is better than an empty string, because then you can distinguish between an attribute that has never been set and an attribute that has been set to an empty string.
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ how-to-check-if-a-string-is-empty-or-none-in-python
How to Check if a String is Empty or None in Python
June 5, 2023 - This concept plays a crucial role when checking conditions in code. For strings, an empty string ("") is considered "falsy" โ€” it evaluates to False in a boolean context. On the other hand, a non-empty string is "truthy" โ€” it evaluates to True. The special value None is also considered "falsy", ...
Find elsewhere
Top answer
1 of 2
15

Checking if a string has characters in it by checking len(str(result)) is definitely not pythonic (see http://www.python.org/dev/peps/pep-0008/).

result = foo() # foo will return None if failure 
if result:
    # deal with result.
    pass

None and '' coerce to the boolean False.


If you are really asking why str(None) does return 'None', then I believe it is because it is necessary for three-valued logic. True, False and None can be used together to determine if a logical expression is True, False or cannot be decided. The identity function is the easiest for representation.

True  -> 'True'
False -> 'False'
None  -> 'None'

The following would be really weird if str(None) was '':

>>> or_statement = lambda a, b: "%s or %s = %s" % (a, b, a or b)
>>> or_statement(True, False)
'True or False = True'
>>> or_statement(True, None)
'True or None = True'
>>> or_statement(None, None)
'None or None = None'

Now, if you really want for an authoritative answer, ask Guido.


If you really want to have str(None) give you '' please read this other question: Python: most idiomatic way to convert None to empty string?

2 of 2
4

Basically, because an empty string isn't a representation of None. None is a special value that is distinct from an empty string or anything else. As described in the docs, str is supposed to

Return a string containing a nicely printable representation of an object.

Basically, str is supposed to return something printable and human-readable. An empty string would not be a readable representation of None.

๐ŸŒ
Java2Blog
java2blog.com โ€บ home โ€บ python โ€บ python string โ€บ print none as empty string in python
Print None as Empty String in Python - Java2Blog
November 18, 2022 - None is a whole data type in itself that is provided by python and it is utilized to define a null value. An object of the None data type can be, however, represented as an empty string in python by converting it into a string data type.
๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ python-convert-none-to-empty-string
Convert None to Empty string or an Integer in Python | bobbyhadz
Use the boolean OR operator to convert None to an empty string in Python, e.g. `result = None or ''`.
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ python-none
Python None: The Standard for Missing or Empty Values | DataCamp
November 10, 2025 - In Python, None is the standard way to represent the absence of a value. It shows up when data is missing, when a variable hasnโ€™t been initialized, or when a function has no meaningful result to return. Rather than leaving ambiguity, None provides a clear signal that something is intentionally ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-convert-none-to-empty-string
Python - Convert None to empty string - GeeksforGeeks
July 12, 2025 - The ternary conditional operator in Python provides a concise way to perform conditional operations within a single line. It can be used for replacing words from a dictionary by checking each word and applying a substitution if the word is found in the dictionary. ... input_value = None # Convert None to empty string using ternary operator result = "" if input_value is None else input_value print(result)
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
.Empty string comprehension? - Python Help - Discussions on Python.org
March 14, 2020 - Hello everyone , I wrote this code ... return person a = person_infos('fna', 'lna', age=34) print(a) The variable age in the function definition is an empty string , but we when set age as a number ( age = 4 ) no error happens , ...
๐ŸŒ
Designcise
designcise.com โ€บ web โ€บ tutorial โ€บ is-an-empty-string-considered-none-in-python
Is an Empty String Considered "None" in Python? - Designcise
November 10, 2023 - It is a valid string object, but it is not the same as None. Here's a simple example to illustrate the difference: def is_none(var): return var is None print(is_none('')) # False print(is_none("")) # False print(is_none(None)) # True ยท In this example, you can see that the variable with the empty string does not equal to None.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_keyword_none.asp
Python None Keyword
Remove List Duplicates Reverse ... None keyword is used to define a null value, or no value at all. None is not the same as 0, False, or an empty string....