Strings can be concatenated in Python. This lets you merge the value of two or more strings into one string. If you try to concatenate a string and a value equal to None, you’ll encounter the “TypeError: unsupported operand type(s) for +: ‘nonetype’ and ‘str’” error.
This guide talks about what this error means and why it is raised. It discusses an example of this error so you can figure out how to fix it in your code.
TypeError: unsupported operand type(s) for +: ‘nonetype’ and ‘str’
The concatenation operator (+) merges string values:
day = "Monday" weather = "sunny" print("It's " + day + " and it is " weather + " outside.")
This code merges the “It’s”, “Monday”, “and it is ”, “sunny”, and “outside.” strings into one long string. The code returns: “It’s Monday and it is sunny outside.”.
The concatenation operator cannot be used to merge values of different data types, such as a string and an integer. This is because the plus sign has different associations with data types like an integer. With integers and floats, the plus sign represents the addition operation.
Values equal to None cannot be concatenated with a string value. This causes the error “TypeError: unsupported operand type(s) for +: ‘nonetype’ and ‘str’”.
An Example Scenario
Let’s build a program that prints out a message displaying information about how much a shoe shop has earned in the last month. This information includes the net and gross income of the store, the value of the highest sale, and the value of the average sale, at the shoe store.
To start, declare a dictionary that stores some values about what the shoe shop has earned in a month:
turnover = { "month": "July", "net_income": 7203.97, "gross_income": 23821.30, "highest_sale": 320.00, "average_sale": 45.00 }
The owner of the shoe store wants to see these values when they run the program. You’re going to use a print statement to access the values about turnover at the store. Use five print statements to display the information in the dictionary:
print("Month: ") + turnover["month"] print("Net income: $") + str(turnover["net_income"]) print("Gross income: $") + str(turnover["gross_income"]) print("Highest sale: $") + str(turnover["highest_sale"]) print("Average sale: $") + str(turnover["average_sale"])
We convert all of the floating-point values in the dictionary to a string. This prevents an error that occurs when you try to concatenate a string and a float. As discussed earlier, only strings can be concatenated to strings. Next, run the code and see what happens:
Month Traceback (most recent call last): File "main.py", line 9, in <module> print("Month") + turnover["month"] TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
The code returns an error.
The Solution
The code successfully prints out the word “Month” to the console. The program stops working when you try to add the value of turnover[“month”] to the “Month” string.
The issue is that you’re concatenating the turnover[“month”] string outside of the print()
statement. The print()
function returns None. Because your concatenation operator comes after the print()
statement, Python thinks that you are trying to add a value to it:
print("Month") + turnover["month"]
To solve this error, we must move the concatenation operation into the print()
statements:
print("Month: " + turnover["month"]) print("Net income: $" + str(turnover["net_income"])) print("Gross income: $" + str(turnover["gross_income"])) print("Highest sale: $" + str(turnover["highest_sale"])) print("Average sale: $" + str(turnover["average_sale"]))
Concatenation operators should always come after a string value, not the print()
statement whose value you want to concatenate. Let’s execute the program:
Month: July Net income: $7203.97 Gross income: $23821.3 Highest sale: $320.0 Average sale: $45.0
Our code successfully executes all of the print()
statements. Each print statement contains a label to which a value from the “turnover” dictionary is concatenated.
Conclusion
The “TypeError: unsupported operand type(s) for +: ‘nonetype’ and ‘str’” error is raised when you try to concatenate a value equal to None with a string. This is common if you try to concatenate strings outside of a print()
statement.
To solve this error, check the values on both sides of a plus sign are strings if you want to perform a concatenation operation.
Now you’re ready to fix this error in your Python program like a professional coder!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.