How to Solve "TypeError: bad operand type for unary +/-: 'str'" in Python
The TypeError: bad operand type for unary +: 'str'
(or -
) error in Python signals an attempt to use the unary plus or unary minus operator on a string. These operators are intended for numerical values, and applying them to strings results in this error.
This guide outlines the common causes of this error and provides practical solutions.
Understanding the Error
The unary plus (+
) and unary minus (-
) operators are designed to operate on numeric values. Using them on strings, which are not numbers, leads to a TypeError
:
a_str = 'tutorialreference'
a_str = +'123' # TypeError: bad operand type for unary +: 'str'
- The plus sign is usually used as an operator between two numeric variables to sum their values. It is not expected to work on a string.
Solutions
To resolve this error, address the underlying issue:
Converting Strings to Numerical Types
If the string represents a number, convert it to an integer or float before applying the unary operator:
a_str = '105'
new_str = +int(a_str) # Convert before applying unary +
print(new_str) # Output: 105
a_str = '105'
new_str = -int(a_str) # Convert before applying unary -
print(new_str) # Output: -105
a_float_str = '105.123'
new_float = +float(a_float_str)
print(new_float) # Output: 105.123
- The
int()
andfloat()
functions are used to convert the string to their numerical types. Then, the unary operator is applied to the result.
Correcting String Concatenation
Ensure you use the correct operator for string concatenation, and make sure that the plus sign is next to the equal sign:
a_str = 'tutorialreference'
a_str += '123' # Corrected - appends to the string
print(a_str) # Output: tutorialreference123
- The
+=
operator will correctly concatenate the strings
Using String Concatenation Operators Correctly
You can concatenate strings using unary plus, but only between two strings:
first = 'tutorial'
last = 'reference'
full_name = first + ' ' + last
print(full_name) # Output: tutorial reference
To concatenate strings you must use two string variables and the unary plus sign between them.
Here's another way to concatenate strings on multiple lines:
first = 'tutorial'
last = 'reference'
# Enclose expression in parenthesis
full_name = (first
+ ' ' + last
)
print(full_name) # Output: tutorial reference
- The code also demonstrates that parenthesis may be used to span the string concat operation across multiple lines.
- If you need to use newline character for formatting, it is preferrable to use f-strings or triple quotes to concatenate strings on multiple lines.
Troubleshooting Context-Specific Errors
In some specific cases, the errors might have particular causes.
Incorrect print()
Usage
Check for accidental use of the unary operator within a print()
function:
first = 'tutorial'
last = 'reference'
print(first, + last) # TypeError: bad operand type for unary +: 'str'
Correct: print(first + ' ' + last)
or print(first, last)
(uses a comma)
input()
Always Returns a String
Remember that input()
always returns a string, even if the user enters a number. You must explicitly convert the input to a number.
num = int(input('Enter a number: ')) # Convert to int
if num > 10:
print(num)
else:
print(-num) # Corrected: No unary operator on string here.
- The unary operators will work as expected only if we are operating on numbers.
Conclusion
The TypeError: bad operand type for unary +: 'str'
(or -
) error arises from using unary operators with strings.
Resolve this error by understanding the types of your variables and converting them to integer or float where necessary, or by correcting the statement where the operator is being used.
By applying these techniques, you can write more robust and error-free Python code.