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
Discussions

python - Using Null/None/Nil vs empty data types - Software Engineering Stack Exchange
However, I would avoid things like empty strings, 0/-1, false as defaults at all costs. None throws an error when used. -1 does not. Errors will silently propagate through your system, that could have been detected early if None was used. ... Python's None object is generally used to indicate ... More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
August 9, 2019
What are blank float inputs defined as? Clearly not zero
When I put nothing into a float input, instead of it returning “num is 0” it returns as completely blank. (I made the code in 30 seconds to demonstrate the problem, and I am relatively new to coding.) num = float(input(… More on discuss.python.org
🌐 discuss.python.org
12
0
October 26, 2023
.Empty string comprehension?
Hello everyone , I wrote this code that I understand except one detail in it , here’s the code : person_infos(first_name, last_name, age=''): person = {'first': first_name, 'last': last_name} if age: per… More on discuss.python.org
🌐 discuss.python.org
4
1
March 14, 2020
Why is it better to write `if a is None` instead of `if a == None`?
As title. I know the first style is recommended by PEP 8, and when I write the second style, PyCharm gives me PEP 8: E711 comparison to None should be 'if cond is None But why it’s better to write is? IMHO the fact that None is a singleton is an implementation detail, and there’s no explaining ... More on discuss.python.org
🌐 discuss.python.org
19
0
December 31, 2023
🌐
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.
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"
🌐
Boot.dev
boot.dev › lessons › 06a78273-7b14-4d5c-b0c1-1883bc065699
Learn to Code in Python: NoneType Variables | Boot.dev
Not all variables have a value. We can make an "empty" variable by setting it to None. None is a special value in Python that represents the absence of a value. It is not the same as zero, False, or an empty string.
🌐
CodeSignal
codesignal.com › learn › courses › python-foundations-for-beginners › lessons › diving-into-python-data-types-numerical-string-boolean-and-none
Diving into Python Data Types: Numerical, String, Boolean, ...
As we reach the abyss in our Python ocean exploration, we witness the somewhat elusive None type. None in Python signifies the absence of a value or a null value, representing a void. It's not the same as 0, False, or an empty string.
Find elsewhere
🌐
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", ...
🌐
Mimo
mimo.org › glossary › python › none-null
Python None: Null in Python | Learn Now
'None' is especially common in programming languages like Python, where the boolean concept is intertwined with null values. 'None' can also be used alongside an empty string for scenarios that differentiate between no value and an explicitly empty one.
🌐
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....
🌐
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 reserved pseudo variable that represents nothing - it’s strictly locked to denote nothing and cannot be aloted a pointer in terms of Memory reference - because it has nothing and is nothing. Empty strings are like Containers.
🌐
GeeksforGeeks
geeksforgeeks.org › python-none-keyword
Python None Keyword - GeeksforGeeks
April 26, 2023 - It is not the same as an empty string, a False, or a zero. It is a data type of the class NoneType object. None in Python Python None is the function returns when there are no return statements.
🌐
TutorialsPoint
tutorialspoint.com › article › What-is-the-most-elegant-way-to-check-if-the-string-is-empty-in-Python
What is the most elegant way to check if the string is empty in Python?
April 21, 2025 - An empty string is a string with zero characters. There are various ways to check if a string is empty, but choosing elegant approaches helps improve code readability. In Python, empty strings are considered falsy, which evaluates to False in ...
🌐
Python.org
discuss.python.org › python help
What are blank float inputs defined as? Clearly not zero - Python Help - Discussions on Python.org
October 26, 2023 - When I put nothing into a float input, instead of it returning “num is 0” it returns as completely blank. (I made the code in 30 seconds to demonstrate the problem, and I am relatively new to coding.) num = float(input(…
🌐
Parseltongue
parseltongue.co.in › the-proper-way-to-check-for-none-in-python
ParselTongue - The Proper Way to Check for None in Python
November 15, 2024 - But if name is None, '', or any other empty value, it will print “Name is empty or None.” ... If you need to check if a variable is specifically None, use is None or is not None.
🌐
Medium
medium.com › @anaesvg › none-undefined-null-e88c641b6f36
None, Undefined & Null. There is data represented by definitive… | by anaintech | Medium
April 17, 2024 - In Python, when there’s no data, we use the term “None.” Imagine an empty box labeled “None.” In JavaScript, it’s like having a box labeled “null” for the same purpose, indicating there’s nothing inside. Let’s say you have a table in a database. You want to show that a certain cell is empty, but it’s not the same as saying it contains a zero or an empty string.
🌐
Python Morsels
pythonmorsels.com › none
None in Python - Python Morsels
January 22, 2024 - Python's None value is used to represent nothingness. None is the default function return value.
🌐
Sourcery
sourcery.ai › blog › python-pandas-compare-to-none
Comparing to None in Python and Pandas
Zero of any numeric type: 0, 0.0, 0j, Decimal(0), Fraction(0, 1) Empty sequences and collections: '', (), [], {}, set(), range(0) Note that an empty string is also considered an empty collection.
🌐
Scaler
scaler.com › home › topics › what is none keyword in python?
What is None Keyword in Python | Scaler Topics
March 28, 2024 - Python defines null objects and variables with the keyword None. The None keyword is used to define a "null value", in simple words no value at all. Please note that None is similar to, or cannot be assumed to be equal to 0, False, or an empty string.
🌐
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 , ...
🌐
Python.org
discuss.python.org › python help
Why is it better to write `if a is None` instead of `if a == None`? - Python Help - Discussions on Python.org
December 31, 2023 - As title. I know the first style is recommended by PEP 8, and when I write the second style, PyCharm gives me PEP 8: E711 comparison to None should be 'if cond is None But why it’s better to write is? IMHO the fact that None is a singleton is an implementation detail, and there’s no explaining ...