Typeerror unsupported operand type s for nonetype and str

One of the errors you might encounter is Typeerror unsupported operand type s for nonetype and str.

This error occurs when you try to concatenate or add a string to a variable that has a value of None.

So in this guide, we will provide you with some practical solutions and examples to solve this error and help you write error-free Python code.

So, let’s dive in and fix that error!

What is typeerror unsupported operand type s for str and str?

The Python “TypeError: unsupported operand type(s) for +: ‘NoneType’ and ‘str'” occurs when we attempt to use the addition (+) operator with a None value.

In simpler terms, the error means that you’re trying to perform an operation (in this case, adding a string to a variable) that doesn’t make sense because one of the operands (the variable) has no value.

To fix the error, make sure you aren’t trying to use the addition (+) operator with a None value and a string.

Here is an example of how the error occurs.

str_1 = None
str_2 = 'world'

# TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
result = str_1 + str_2

In this code, the variable on the left-hand side of the addition operator stores a None. Where in it trying to concatenate a None value of a string which triggers the error.

Common cause of this error

In order to fix this error, we should know where the variable got assigned a None value, hence correcting the assignment.

Here are the common factors where the None values are present:

  1. When the function does not return anything.
  2. A variable set to None.
  3. When a function only returns a value if the condition is met.
  4. Assigning a variable to built-function which does not return anything.

How is Typeerror unsupported operand type s for str and str

This time we will give the solution to several factors raising the Typeerror unsupported operand type s for str and str error.

📌Function does return anything

In order to avoid the error, ensure that you are not using a function which does not return anything, where it is expecting to have an output of string value.

Suppose we have the following code:

# this function returns None
def get_str():
    print('Welcome ')

str_2 = 'Coders'

# TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
result = get_str() + str_2

This code will give an error that state…

Traceback (most recent call last):
File “C:\Users\Windows\PycharmProjects\pythonProject1\main.py”, line 9, in
result = get_str() + str_2
TypeError: unsupported operand type(s) for +: ‘NoneType’ and ‘str’

It happens because get_str function doesn’t return anything, hence it returns None.

In order to fix this, we can use return statement, so that it will return a value from a function.

Here is the example code:

def get_str():
    return 'WELCOME '

str_2 = 'CODERS !'

result = get_str() + str_2

print(result)

In this code, the function will now return a string. Aside from that, we can now concatenate strings using the + operators.

Here is the output:

WELCOME CODERS !

📌Print() Function

Another factor also to reproduce this error is using print() function which return None.

For example:

# TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
print('WELCOME') + 'CODERs!'

Since the print() function returns None, therefore the error is triggered.

Traceback (most recent call last):
File “C:\Users\Windows\PycharmProjects\pythonProject1\main.py”, line 2, in
print(‘WELCOME’) + ‘CODERs!’

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

It happened because the addition operator is used along with None value and string.

To fix this error, we need to move string inside the parentheses like this:

print('WELCOME' + ' CODERS!')

Output:

WELCOME CODERS!

Alternatively, we can use formatted string literal, like this:

my_var1= 'WELCOME'
my_var2 = 'CODERS!'

result = f'{my_var1} {my_var2}!!'
print(result)

Output:

WELCOME CODERS!!!

📌Check if the variable is None

Check if the variable is None before performing the concatenation or addition operation.

Example code:

name = None
if name is not None:
    greeting = "Hello, " + name
    print(greeting)
else:
    print("Name is not set")

Explanation:

This solution checks whether the variable is None before performing the concatenation operation. If the variable is not None, the concatenation operation is performed to create the greeting variable, and it is printed.

If the variable is None, the code prints a message saying that the name is not set.

Output:

Name is not set

Anyway, we also have a solution for Typeerror series objects are mutable thus they cannot be hashed error, you might encounter.

Conclusion

To conclude, TypeError: unsupported operand type(s) for +: ‘NoneType’ and ‘str'” typically occurs when you try to concatenate or add a string and a variable, but the variable has a value of None.

In order to fix this error we need to make sure that the variable we are trying to concatenate or add with a string has a valid value.

We hope this guide helped you fix your error and get back on your coding.

Thank you for reading! 😊