Last updated: Apr 11, 2024
Reading time·4 min
+
to concatenate strings\
when concatenating multiline stringsprint()
incorrectlyinput()
function always returns a stringThe Python "TypeError: bad operand type for unary +: 'str'" and
"TypeError: bad operand type for unary -: 'str'" errors occur when you try
to use the unary +
or -
operators with a string.
To solve the error, convert the value to an integer or a float by using the
int()
or float()
class or correct the assignment.
Here is an example of how the error occurs.
a_str = 'bobbyhadz' a_str = +'123' # ⛔️ TypeError: bad operand type for unary +: 'str' print(a_str)
And here is another example.
a_str = '105' new_str = +a_str # ⛔️ TypeError: bad operand type for unary +: 'str' print(new_str)
We are trying to use the unary +
operator with a string which is not allowed.
You might also get the error when using the unary -
operator.
a_str = '105' new_str = -a_str # ⛔️ TypeError: bad operand type for unary -: 'str' print(new_str)
One way to solve the error is to convert the value to an integer or a
floating-point number before using the unary +
and -
operators.
a_str = '105' new_str = +int(a_str) print(new_str)
The code sample uses the int() class to convert the value to an integer before using the unary plus operator.
You can also convert the value to a floating-point number before using the operator.
a_str = '105.123' new_str = +float(a_str) print(new_str) # 👉️ 105.123
We used the float() class to convert the string to a
floating-point number before using the unary +
operator.
The same approach can be used to solve the "TypeError: bad operand type for unary -: 'str'" error.
a_str = '105' new_str = -int(a_str) print(new_str) # 👉️ -105
The error is also raised when you have an incorrect assignment when concatenating strings.
a_str = 'bobbyhadz' a_str = +'123' # ⛔️ TypeError: bad operand type for unary +: 'str' print(a_str)
We tried to append the string "123"
to the string "bobbyhadz"
, however, the
plus should be placed to the left of the equal sign =
.
a_str = 'bobbyhadz' a_str += '123' print(a_str) # 👉️ bobbyhadz123
The expression a_str += 'abc'
is equivalent to a_str = a_str + 'abc'
.
a_str = 'bobbyhadz' a_str += '123' print(a_str) # 👉️ bobbyhadz123 # Same as above new_str = 'bobbyhadz' new_str = new_str + '123' print(new_str) # 👉️ bobbyhadz123
+
to concatenate stringsIf you meant to use the unary +
operator to concatenate strings, use the
following syntax.
first = 'bobby' last = 'hadz' full_name = first + ' ' + last print(full_name) # bobby hadz
The unary +
operator is used between two strings.
You can also use a formatted string literal to achieve the same result.
first = 'bobby' last = 'hadz' full_name = f'{first} {last}' print(full_name) # bobby hadz
Formatted string literals (f-strings) let us include expressions inside of a
string by prefixing the string with f
.
Make sure to wrap expressions in curly braces - {expression}
.
\
when concatenating multiline stringsMake sure to use a backslash when concatenating multiline strings.
first = 'bobby' last = 'hadz' full_name = first \ + ' ' + last print(full_name) # 👉️ bobby hadz
The \
backslash character is used as a line continuation character and
instructs Python that the expression continues on the next line.
Alternatively, you can use parentheses ()
.
first = 'bobby' last = 'hadz' full_name = (first + ' ' + last ) print(full_name) # 👉️ bobby hadz
Either approach works, but using parentheses is a bit easier to read.
print()
incorrectlyThe error also occurs when you use the print()
function incorrectly.
first = 'bobby' last = 'hadz' # ⛔️ TypeError: bad operand type for unary +: 'str' print(first, + last)
We are trying to use the unary +
operator with a string.
Notice that we have a comma ,
and a plus +
sign.
To solve the error, we either have to remove the comma or the +
sign.
first = 'bobby' last = 'hadz' print(first, last) # 👉️ bobby hadz print(first + last) # 👉️ bobbyhadz
input()
function always returns a stringNote that the input()
function always returns a string.
You have to use the int()
class to convert the string to an integer when
taking integer user input.
num = int(input('Enter a number: ')) if num > 10: print(num) else: print(-num)
We used the int()
class to convert the input string to an integer but you can
also use the float()
class if you need to convert the value to a
floating-point number.
We are no longer trying to use the unary -
operator with a string, so the
error is resolved.
You can learn more about the related topics by checking out the following tutorials: