TypeError: bad operand type for unary +: 'str' [Solved]

avatar
Borislav Hadzhiev

Last updated: Apr 11, 2024
4 min

banner

# Table of Contents

  1. TypeError: bad operand type for unary +: 'str' [Solved]
  2. Convert the value to an integer or float before using the unary + and - operators
  3. Correct your assignments when concatenating strings
  4. Using + to concatenate strings
  5. Use a backslash \ when concatenating multiline strings
  6. Using print() incorrectly
  7. The input() function always returns a string

# TypeError: bad operand type for unary +: 'str' [Solved]

The 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.

main.py
a_str = 'bobbyhadz' a_str = +'123' # ⛔️ TypeError: bad operand type for unary +: 'str' print(a_str)

type error bad operand type for unary str

And here is another example.

main.py
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.

main.py
a_str = '105' new_str = -a_str # ⛔️ TypeError: bad operand type for unary -: 'str' print(new_str)

# Convert the value to an integer or float before using the unary + and - operators

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.

main.py
a_str = '105' new_str = +int(a_str) print(new_str)

convert the value to integer or float before using unary plus

The code for this article is available on GitHub

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.

main.py
a_str = '105.123' new_str = +float(a_str) print(new_str) # 👉️ 105.123

convert the value to integer or float before using unary plus

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.

main.py
a_str = '105' new_str = -int(a_str) print(new_str) # 👉️ -105

convert-to-int-before-using-unary-minus

# Correct your assignments when concatenating strings

The error is also raised when you have an incorrect assignment when concatenating strings.

main.py
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 =.

main.py
a_str = 'bobbyhadz' a_str += '123' print(a_str) # 👉️ bobbyhadz123

move plus to left of equal sign

The code for this article is available on GitHub

The expression a_str += 'abc' is equivalent to a_str = a_str + 'abc'.

main.py
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

equivalent assignments

# Using + to concatenate strings

If you meant to use the unary + operator to concatenate strings, use the following syntax.

main.py
first = 'bobby' last = 'hadz' full_name = first + ' ' + last print(full_name) # bobby hadz

using unary plus to concatenate strings

The code for this article is available on GitHub

The unary + operator is used between two strings.

You can also use a formatted string literal to achieve the same result.

main.py
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}.

# Use a backslash \ when concatenating multiline strings

Make sure to use a backslash when concatenating multiline strings.

main.py
first = 'bobby' last = 'hadz' full_name = first \ + ' ' + last print(full_name) # 👉️ bobby hadz
The code for this article is available on GitHub

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 ().

main.py
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.

# Using print() incorrectly

The error also occurs when you use the print() function incorrectly.

main.py
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.

main.py
first = 'bobby' last = 'hadz' print(first, last) # 👉️ bobby hadz print(first + last) # 👉️ bobbyhadz
The code for this article is available on GitHub

# The input() function always returns a string

Note 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.

main.py
num = int(input('Enter a number: ')) if num > 10: print(num) else: print(-num)

convert input to integer

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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.