Borislav Hadzhiev
Last updated: Apr 20, 2022
Check out my new book
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.
Here is an example of how the error occurs.
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.
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:
None
implicitly).None
.Here is an example of getting a None
value from a function that doesn't return
anything (implicitly returns None
).
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.
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
.
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
.
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.
name = None result = f'hello {name}' print(result) # 👉️ 'hello None'
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.
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
.
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.
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.