How To Solve “Unsupported Operand Type(s) For +: ‘NoneType’ And ‘Str'” Error

TypeError: unsupported operand type(s) for +: ‘NoneType’ and ‘str’

Suppose you are looking for a simple method to fix error “Unsupported operand type for +: ‘NoneType’ and ‘str'”. Stay tuned for our posts. We will give you some practical solutions to fix this error.

Reproduce The Error “Unsupported operand type(s) for +: ‘NoneType’ and ‘str'”

“Unsupported operand type(s) for +: ‘NoneType’ and ‘str'” is the error message that appears when we try to perform the ‘+’ operator between the value None and a string. ‘NoneType’ is the data type of the value None, and ‘str’ is the data type for string values. The ‘+’ operator can only concatenate two string values, and if either value is a string, not a string, Python will throw an error.

The below code will print the error Unsupported operand type(s) for +: ‘NoneType’ and ‘str’.

Example 1:

my_string_1 = None
my_string_2 = 'string'
result = my_string_1 + my_string_2

Output:

TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

The variable ‘my_string_1’ contains the None value, so trying to add a None value to the string will cause this error.

Example 2:

def value_1():
  	print('my')

value_2 = 'string'
value = value_1() + value_2

Output:

TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

How To Solve The Error?

Whenever you see this error in your Python program, you must check your code and ensure that you are not concatenating any None and string values. Here I will give two ways to solve the error.

Use an if statement to check if a variable holds the value None before using the add ‘+’ operator.

Example 1: 

my_string_1 = None
my_string_2 = 'string'

if my_string_1 is None:
  	my_string_1 = ''

result = my_string_1 + my_string_2
print(result)

Output:    

string

We will use the if statement to see if the string ‘my_string_1’ is an empty value; if is None, we will set it to the empty string. Thanks to that, the error is solved.

Example 2:

We can use ‘return’ keyword to return the desired value from a function.

def value_1():
  	return 'my'

value_2 = 'string'
value = value_1() + value_2
print(value)

Output:    

 mystring

You can learn more about using the ‘return’ function here.

Example 3:

Another cause of this error is having a function that only returns a value if another condition is met. I will show you how to solve the error in this case as follows:

def check_none(value_1):
    if len(value_1) > 6:
    	return value_1
    return ''

value_1 = check_none('my')
value_2 = 'string'
result = value_1 + value_2
print(result)

Output:  

string

Summary

The error “TypeError “unsupported operand type(s) for +: ‘NoneType’ and ‘str'” occurs when we try to add an empty value with a string. To fix this error, you need to find the variable assigned to None and improve the set error.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *