typeerror: bad operand type for unary +: ‘str’ [SOLVED]

In this, tutorial, we will discuss the solutions on how to solve the bad operand type for unary +: str and Why the error occurs.

Before we proceed to the solutions to solve the error, we will know first why the error occurs.

Why the error bad operand type for unary +: ‘str’

The TypeError: bad operand type for unary +: ‘str’ occurs when you prepend the unary plus operator (+) on a string value. The unary plus operator(+) shows a positive value, and it is suggested to only be used numerical data types such as int and float values.

For an example of an error occurs:

a = "19"
b = +a

print(b)

Output:

C:\Users\Dell\PycharmProjects\pythonProject\venv\Scripts\python.exe C:\Users\Dell\PycharmProjects\pythonProject\main.py
Traceback (most recent call last):
File “C:\Users\Dell\PycharmProjects\pythonProject\main.py”, line 2, in
y = +x
TypeError: bad operand type for unary +: ‘str’

In the code example, we prepend the unary + operator to a string that isn’t correct, so that’s why it throws TypeError because it doesn’t have a meaning to prepend the + operator on a string value.

Also read: typeerror: descriptors cannot not be created directly [SOLVED]

Some of the common causes include

  • You are trying to use the unary + operator with a string value.
  • You are using a variable that contains a string value in a mathematical expression.
  • You are using the wrong data type in your code.

How to solve the typeerror: bad operand type for unary +: str?

To solve the TypeError: bad operand type for unary +: ‘str’ error in Python, we will convert the string to a numerical value by using the int() or float() function.

For example:

x = "50"

x_int = int(x)

y = +x_int

print(y)

Output:

C:\Users\Dell\PycharmProjects\pythonProject\venv\Scripts\python.exe C:\Users\Dell\PycharmProjects\pythonProject\main.py
50

In this code example, we converted the string “50” to an integer by using the int() function and then we used the unary plus operator(+) on the integer to display the positive value.

The unary + operator in Python displays the positive value of a numeric expression.

The unary + operator will execute no actual mathematical operation yet it will return the numeric value.

The another solution to solve this error is that we will be using the concatenate + operator on Strings in Python.

For concatenating strings in Python, we can use the + operator.

x = "50"
y = "61"

z = x + y

print(z)

Output:

C:\Users\Dell\PycharmProjects\pythonProject\venv\Scripts\python.exe C:\Users\Dell\PycharmProjects\pythonProject\main.py
5061

As you can see that we are using the unary plus operator(+) on strings, yet it doesn’t give you the TypeError because we’re using it to concatenate a string; we aren’t covering that operator on a string.

Concatenating a string and prepending the unary plus operator is peculiar in programming.

If you’re trying to add two numeric values and getting the TypeError, That means one of those values could be a string or at least perform like a string. Make sure that both values are numeric before performing the addition.

FAQs

Can the “typeerror: bad operand type for unary +: ‘str’” error message occur in any programming language?

No, the “typeerror: bad operand type for unary +: ‘str’” error message is specific to Python.

What is Unary + Operator?

The Unary + operator is used to convert a value into a positive number. For example, if you have a variable with a negative value, you can use the Unary + operator to convert it into a positive value.

Understanding “unsupported operand type” TypeErrors

Every operator (+, -, *, /) is defined on specific type pairs. Python does NOT auto-convert types like JavaScript does. Adding an integer to a string, or comparing a list to a dict, raises TypeError.

Common triggers

  • int + str. "Age: " + 25 fails. Use f-string: f"Age: {25}".
  • str * float. "ab" * 2.5 fails. Only int multiplication is allowed on strings.
  • None arithmetic. Any arithmetic on None gives an unsupported-operand error.
  • Comparing dict to list. Python 3 raises TypeError on many cross-type comparisons (unlike Python 2).
  • Path + str. pathlib.Path + string works only with / (division operator), not +.

Diagnostic pattern

# BAD — mixing int and str
count = 5
msg = "You have " + count + " items"
# TypeError: can only concatenate str (not "int") to str

# GOOD — three modern ways
msg = f"You have {count} items"       # f-string (Python 3.6+)
msg = "You have {} items".format(count)  # .format()
msg = "You have " + str(count) + " items"  # explicit conversion

Best practices

  • Prefer f-strings. Cleaner and faster than concatenation or .format().
  • Use str() explicitly when you need a string representation of any non-string value.
  • Use Decimal for money. Mixing float and Decimal in arithmetic often causes surprises.

Frequently Asked Questions

What is Python TypeError and what causes it?

TypeError is raised when an operation is applied to an object of the wrong type. Common patterns: calling a non-callable object, adding incompatible types (str + int), passing the wrong number of arguments, or accessing attributes on a NoneType. Each TypeError message names the operation and expected vs actual types, the fix is almost always to convert types explicitly (int(), str()) or fix the wrong variable assignment.

How do I quickly debug a Python TypeError?

Three steps: (1) Read the full error message, it names the exact operation and types involved. (2) Print the type of every variable in that line: print(type(var1), type(var2)). (3) Check what the function expected vs what you passed. Most TypeError fixes are 1-line type casts or fixing a variable that became None unexpectedly.

Should I catch TypeError or let it propagate?

For internal code, let TypeError propagate, it’s almost always a real bug (wrong type passed). For boundary code (parsing user input, third-party API responses), catch TypeError + ValueError together: try: parsed = int(value) except (TypeError, ValueError): parsed = 0. Catching internal TypeErrors hides bugs.

How do I prevent TypeError in production?

Three patterns: (1) Use type hints (def add(a: int, b: int) -> int) and check with mypy / pyright in CI. (2) Validate inputs at boundaries (Pydantic for FastAPI, DRF serializers for Django). (3) Default values that match expected types (return 0 not None for numeric functions). Static typing catches 80% of TypeErrors before runtime.

Where can I find more TypeError fixes?

Browse the TypeError reference hub for 220+ specific TypeError fixes. For broader Python debugging, see the Python Tutorial hub. For related error types, see ValueError and AttributeError guides.

Conclusion

In conclusion, this article will be able to help you to resolve the error typeerror: bad operand type for unary +: ‘str’ you may encounter while working in python programming.

Adones Evangelista


Programmer & Technical Writer at PIES IT Solution

Adones Evangelista is a programmer and writer at PIES IT Solution, author of over 900 tutorials and error-fix guides at itsourcecode.com. Specializes in JavaScript, Django, Laravel, and Python error debugging covering ValueError, TypeError, AttributeError, ModuleNotFoundError, and RuntimeError, plus C/C++ and PHP capstone projects for BSIT students.

Expertise: JavaScript · Python · Django · Laravel · Error Debugging · C/C++
 · View all posts by Adones Evangelista →

Leave a Comment