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

In this article, we will provide solutions to typeError: bad operand type for unary ‘str’, example codes, and a discussion about this error.

What is unary operators?

Unary operators are operators that take only one operand. In Python, some of the commonly used unary operators include the positive and negative signs (+ and -). Then, the bitwise NOT operator (~), and the logical NOT operator (not).

These operators are often used to perform mathematical operations or to invert boolean values.

Bad operand type error

The bad operand type error occurs when the operand passed to a unary operator is of the wrong data type.

For instance, applying the logical NOT operator to a non-boolean value would result in a bad operand type error.

TypeError: bad operand type for unary ‘str’

In Python, TypeError is a common error that occurs when the data types used in an operation are not compatible.

One specific variation of this error is the “bad operand type for unary ‘str‘” error. This error indicates that the code is trying to apply a unary operator to a variable or value of the wrong data type.

Example of TypeError: bad operand type for unary ‘str’

One example of type error is TypeError: bad operand type for unary +: ‘str’ it is raised when we use the unary plus operator (+) on a string value.

The unary plus operator(+) shows a positive value, and it is recommended to only be used with numerical data types such as int and float values.

Example error program:

a = "20"
b = +a

print(b)

Result:

TypeError: bad operand type for unary +: 'str'

How to fix TypeError: bad operand type for unary ‘str’

Here are the solutions to fix the given TypeError: bad operand type for unary ‘str’ error above.

1. Convert a string to numerical value using int or float

The first thing we should do to fix typeError: bad operand type for unary +: ‘str’ is to convert the string to numerical value.

Example program:

sample = "2023"

sample_int = int(sample)

result = +sample_int

print(result)

Result:

2023

The code above demonstrates the conversion of string ‘2023’ into an integer using the int function. Along with it uses the unary + plus operator to show a positive value.

Primarily, that unary + operator shows the positive value of a numerical expression. Although it doesn’t perform actual mathematical operations hence it returns the numerical value as is.

2. Use concatenate + on string

This time we will use + operator on a string. It will not cause an error TypeError: bad operand type for unary ‘str’ since we use it to concatenate a string, assumingly we are not using an operator in string.

Meanwhile, if we are trying to add two numeric values but typeerror is thrown. This means one of the value is a string or behave like a string. That is why make sure to have numeric values before executing addition.

Here is the example code:

x = "20"
y = "23"

z = x + y

print(z)

Here is the output using concatenation.

2023

3. Check the type of the object before performing the operation:

You can use the type() function to check the type of the object before performing the string operation.

x = 5
if type(x) == str:
    print("x is a string")
else:
    print("x is not a string")

Output:

x is not a string

Conclusion

In conclusion, the “bad operand type for unary ‘str'” error occurs when attempting to use a unary operator on a value of the wrong data type. Understanding the causes of this error and how to properly handle string operations can help you avoid this common issue in Python.

We hope that this article has helped you resolve this error and that you can now continue working on your Python projects without any issues.

If you are finding solutions to some errors you might encounter we also have Typeerror: can’t compare offset-naive and offset-aware datetimes.

Leave a Comment