TypeError: can only concatenate str (not "NoneType") to str

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Check out my new book

TypeError: can only concatenate str (not "NoneType") to str #

The Python "TypeError: can only concatenate str (not "NoneType") to str" occurs when we try to concatenate a string and a None value. To solve the error, correct the assignment or check if the variable doesn't store a None value before concatenating.

typeerror can only concatenate str not nonetype to str

Here is an example of how the error occurs.

main.py
example = None # ⛔️ TypeError: can only concatenate str (not "NoneType") to str result = 'hello ' + example

We tried to use the addition (+) operator to concatenate a string and a None value which caused the error.

To solve the error, we either have to figure out where the variable got assigned a None value and correct the assignment or check if the variable doesn't store None before concatenating.

The most common sources of a None value are:

  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.

Here is an example of getting a None value from a function that doesn't return anything (implicitly returns None).

main.py
def get_name(): print('James Doe') # ⛔️ TypeError: can only concatenate str (not "NoneType") to str result = 'hello ' + get_name()

The get_name function doesn't return anything, so it implicitly returns None.

You can use a return statement to return a value from the function.

main.py
def get_name(): return 'James Doe' result = 'hello ' + get_name() print(result) # 👉️ "hello James Doe"

You can use an if statement if you only want to concatenate the values if the variable doesn't store None.

main.py
name = None if name is not None: result = 'hello ' + name print(result) else: # 👉️ this runs print('variable stores a None value')

The if block is only ran if the name variable doesn't store a None value, otherwise the else block is ran.

Alternatively, you can assign a fallback value to the variable if it is None.

main.py
name = None if name is None: name = '' result = 'hello ' + name print(result) # 👉️ 'hello'

We check if the name variable stores a None value, and if it does, we set it to an empty string.

Note that if you use a formatted string literal with an expression that returns None, the None value will be included in the string.

main.py
name = None result = f'hello {name}' print(result) # 👉️ 'hello None'
Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.

Make sure to wrap expressions in curly braces - {expression}.

Note that there are many built-in methods that mutate the original object (e.g. sort()) and therefore don't return anything (implicitly return None), so make sure you aren't storing the result of calling one in a variable.

Another common source of None values is having a function that only returns a value if a certain condition is met.

main.py
def get_name(a): if len(a) > 3: return a result = get_name('Bob') print(result) # 👉️ None

The if statement in the get_name function is only ran if the passed in argument 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_name(a): if len(a) > 3: return a return '' # 👈️ return empty string if condition not met result = get_name('Bob') print(result) # 👉️ ""

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

Use the search field on my Home Page to filter through my more than 3,000 articles.