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....
๐ŸŒ
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.
๐ŸŒ
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 ("") ...
๐ŸŒ
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
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ python โ€บ check if a string is empty in python
Check if a string is empty in Python | Sentry
The expression in our if statement above would also evaluate to True if my_string contained the boolean value False, or even an empty list. If we want to ensure that my_string is an empty string and not a different false-equivalent value, we should do the following: ... if my_string == "": print("my_string is an empty string!") else: print("my_string is not an empty string!")
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.

๐ŸŒ
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 , ...
๐ŸŒ
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 ...
๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ python-convert-none-to-empty-string
Convert None to Empty string or an Integer in Python | bobbyhadz
The function takes a value as a parameter and returns an empty string if the supplied value is None. If the value is not None, the value is converted to a string and passed to the str() class.
๐ŸŒ
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....
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ difference between value=none and value=""
r/learnpython on Reddit: Difference between value=None and value=""
September 8, 2020 -

Can someone please explain in a technical, yet still understandable for beginner, way what's the difference between those?

I can see in PCC book that author once uses example

def get_formatted_name(first_name, second_name, middle_name=""):

but on the next page with another example is this:

def build_person(first_name, last_name, age=None):

from what I read in that book, doesn't seem like there is a difference, but after Googling seems like there is, but couldn't find any article that would describe the differences clearly.

Thank you all in advance.

๐ŸŒ
Esri Community
community.esri.com โ€บ t5 โ€บ python-questions โ€บ quot-if-not-null-and-not-empty-string-quot-paradox โ€บ td-p โ€บ 1618009
Solved: "If not Null and not empty string" paradox - help ... - Esri Community
May 29, 2025 - Your original logic is also evaluating to True if the sate is both None and not an empty string (which is always True) ... From both a practice ( https://peps.python.org/pep-0008/ ) and performance perspective, checking for None should be done using "is" or "is not".
๐ŸŒ
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.