AttributeError: 'NoneType' object has no attribute 'format'

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Check out my new book

AttributeError: 'NoneType' object has no attribute 'format' #

The Python "AttributeError: 'NoneType' object has no attribute 'format'" occurs when we try to call the format() method on a None value, e.g. assignment from function that doesn't return anything. To solve the error, make sure to only call format() on strings.

attributeerror nonetype object has no attribute format

Here is a very simple example of how the error occurs.

main.py
my_string = None # ⛔️ AttributeError: 'NoneType' object has no attribute 'format' print(my_string.format('hello', 'world'))

Trying to call the format() method on a None value is what causes the error.

Make sure you aren't calling the format() method after using print().

main.py
my_string = '{} {}' # ⛔️ AttributeError: 'NoneType' object has no attribute 'format' print(my_string).format('hello', 'world')

The issue in the code sample is that we are calling the format() method on the result of calling print(), which returns None.

Instead, call the format() method on the string.

main.py
my_string = '{} {}' print(my_string.format('hello', 'world')) # 👉️ "hello world"
If you print the variable you are calling format() on, it will be None, so you have to track down where the variable gets assigned a None value and correct or remove the assignment.

The most common source of a None value (other than an explicit assignment) is a function that doesn't return anything.

main.py
# 👇️ this function returns None def get_string(): print('{} {}') # 👇️ None my_string = get_string() # ⛔️ AttributeError: 'NoneType' object has no attribute 'format' print(my_string.format('hello', 'world'))

Notice that our get_string function doesn't explicitly return a value, so it implicitly returns None.

We assigned the result of calling the function to a variable and tried to call format which caused the error.

The "AttributeError: 'NoneType' object has no attribute 'format'" occurs for multiple reasons:

  1. Having a function that doesn't return anything (returns None implicitly).
  2. Explicitly setting a variable to None.
  3. Assigning a variable to the result of calling a built-in function that doesn't return anything.
  4. Having a function that only returns a value if certain condition is met.

If a variable might sometimes store a string and sometimes store None, you can explicitly check if the variable is not None before you call format().

main.py
my_string = '{} {}' if my_string is not None: print('Variable is not None') result = my_string.format('hello', 'world') print(result) # 👉️ "hello world" else: print('Variable is None')

The if block will run only if the my_string variable does not store a None value, otherwise the else block runs.

Another common cause of the error is having a function that returns a value only if a condition is met.

main.py
def get_string(a): if len(a) > 3: return a my_string = get_string('hi') print(my_string) # 👉️ None

The if statement in the get_string function is only ran if the passed in string has a length greater than 3.

In all other cases, the function doesn't return anything and ends up implicitly returning None.

To solve the error in this scenario, you either have to check if the function didn't return None or return a default value if the condition is not met.

main.py
def get_string(a): if len(a) > 3: return a return '' my_string = get_string('hi') print(my_string) # 👉️ ""

Now the function is guaranteed to return a string regardless if the condition is met.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.