We can use the + operator in Python to add two numbers and merge two string values. But if we perform the + operator between two unsupported data type objects, we will encounter the TypeError with some error message.
Similarly, if we use the + operator between a string and a value that equals None, we receive the error:
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
.
This Python tutorial helps you understand this error, why it occurs, and how to resolve it, with an example.
So without further ado, let's get started!
Python TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
We can use the + symbol as a concatenation operator between two string operands.
Here is an example:
a = 'Hello '
b = 'World!'
c = a + b
print(c)
Output
Hello World!
You cannot use the + operator with operands of different data types. The reason is the operator works differently with integer and float data types, i.e., it performs the addition.
If the operand on the left side of the + operator is a
None
value, and the operand on the right hand is
str
, the Python interpreter will throw the
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
Error.
The Error statement
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
has two parts.
- TypeError (Exception Type)
- unsupported operand type(s) for +: 'NoneType' and 'str'
1. TypeError
TypeError is one of Python's standard Exceptions. It raises in Python when you perform an unsupported operation between two data type objects. It can also rise if we pass an invalid data type object value as an argument to a function or method.
2. unsupported operand type(s) for +: 'NoneType' and 'str'
The "unsupported operand type(s) for +: 'NoneType' and 'str'" is the error message that arises when we use the + operator between a None and a string value. After reading this error message, you can get a clear idea that the + operator is unsupported between 'NoneType' and 'str'.
NoneType is the data type of None value, and str is the data type for string values. The + operator can concatenate only two string values, and if either of the values is a non-string, Python raises the TypeError with an appropriate error message.
Example
# none value
none = None
# str value
string = "Hello"
print(none+string)
Output
Traceback (most recent call last):
File "main.py", line 7, in
print(none+string)
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
Common Example Scenario
Let's write a program that concatenates a person's first and last name and prints the full name on the console window.
Example
rohan = {'First Name': 'Rohan',
'Last Name': 'Kumar',
'Salary': '55,000',
'Bonus' : '2000'
}
full_name = rohan.get('first Name') + rohan.get('Last Name')
print("The full Name is:", full_name)
Output
Traceback (most recent call last):
File "main.py", line 8, in
full_name = rohan.get('first Name') + rohan.get('Last Name')
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
Break the code
In the above example, we received the TypeError error because, in line 8, when we tried to access the First Name key's value of the
rohan
dictionary using the
get()
method. We have passed the
'first Name
' as a key name.
But in the
rohan
dictionary, there is no key name
'first Name'
. The key that we were supposed to pass was
'First Name'
with uppercase
F
. When the
get()
method did not find the key
first Name
in the
rohan
dictionary, it returned
None
.
When the Python interpreter tries to concatenate the
None
value with the result returned by the
rohan.get('Last Name')
statement, it raised the error:
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
.
Solution
Whenever you see this error in your Python program, check your code and look for the line where the Python interpreter shows the error. In that particular line of code, you must ensure that you are not concatenating any None and string values.
Regarding our above example, we have passed the wrong key name to the get() method and received the None value.
To solve our example error, we only need to pass the correct key name to the
get()
method.
Example Solution
rohan = {'First Name': 'Rohan',
'Last Name': 'Kumar',
'Salary': '55,000',
'Bonus' : '2000'
}
full_name = rohan.get('First Name') + " "+ rohan.get('Last Name')
print("The full Name is:", full_name)
Output
The full Name is: Rohan Kumar
You can also convert the None object to an str object using the str() function. But it will concatenate the None as a string value to your string.
Conclusion
The TypeError " unsupported operand type(s) for +: 'NoneType' and 'str'" is raised in a Python program when we use the + operator between a None and a string value. This is a prevalent error you encounter as a beginner.
To fix this error, you must check the value of both the operands on which you performed the + operator. And after looking at the error line, make the correct decision to solve the error.
It is not always the case that you receive this error because you have misspelled the key name, as shown in the above example. There might be another scenario where you receive a None value for the first operand.
If you still get this error in your program, you can share your code and query in the comment section. We will try to help you with debugging .
People are also reading:
- Python TypeError: can only join an iterable Solution
- Detect Shapes in Python using OpenCV
- Python SyntaxError: non-default argument follows default argument Solution
- Make a Chat Room Application in Python
- Python List Files in a Directory: A Complete Guide
- Control your Mouse with Python mouse Module
- Python typeerror: ‘str’ object is not callable Solution
- Read File in Python
- Python SyntaxError: positional argument follows keyword argument Solution
- Make a Subdomain Scanner in Python
Leave a Comment on this Post