. Advertisement .
. Advertisement .
While working with Python programming language, you might have got concatenation for combining double values. The concatenation operator “+” is put for that. However, while doing so, the popular error which is run into is “typeerror: unsupported operand type(s) for +: ‘int’ and ‘str’“. This occurs during the double or a lot of values which you are attempting to insert are not the similar data type.
The ideal solution to correct it is to insert more values that get a similar data type. You could even transform the data types to stay away from the error. In this post, we will summarize and glance at the typical case in which you can come across that error. Then, we will propose some solutions to fix it.
When typeerror: unsupported operand type(s) for +: ‘int’ and ‘str’ occur?
The Python typeerror: unsupported operand type(s) for +: ‘int’ and ‘str’ happens when the integer value is added by a string that might include a valid integer value.
We will show you a basic example of how the error happens.
sample_integer = 1878 sample_string = '1892' sum = sample_integer + sample_string print(sum)
Traceback (most recent call last): File "code.py", line 4, in <module> result = sample_integer + sample_string TypeError: unsupported operand type(s) for +: 'int' and 'str'
Next, we attempt to use the
(+) operator with a combination of the number and the string.
The error is because of the data type’s mismatch. In the other words, operators in python assist similar data types’ operations. When the operator is taken for double various data types, this type of mismatch error can be eliminated.
In a program, if double separate data types, like the integer and string, are used by the
(+) operator, they can initially be transformed to a similar data type, and then an extra operation can be implemented.
1. Use the ‘int()’ class
To tackle the error, we get to transform the string to the number (an ‘int’, or the ‘float’)
sample_integer = 1878 sample_string = '1892' sum = sample_integer + int(sample_string) print('Result is:',sum)
Result is: 3770
Then, we utilized the ‘
int()’ class to transform the string into an integer before using the
Warning: If you take the ‘
input()’ integrated function, all values the client types get converted to strings (even the numeric values).
2. Use the ‘float()’ class
If you get the float included in the string, we will highly recommend that you should use the ‘float()’ class as well.
integer1 = 1878 string1 = '1892' the_Result = integer1 + float(string1) print('The resuilt is:',the_Result)
The resuilt is: 3770.0
3. Use the ‘string()’ class
If you are attempting to concatenate strings, please transform the number to the string by taking the ‘
integer2 = 144 string2 = 'years' the_Result1 = str(integer2) + string2 print(the_Result1)
4. Use the ‘filter()’ function
On top of it, if you get the string which can even consist of characters; but you only want to extract the integer, or take the ‘
filter()’ function to filter out the non-digits.
integer3 = 1879 string3 = 'python19intstr02' integer4 = int(''.join(filter(str.isdigit, string3))) the_Result2 = integer3 + integer4 print(integer4) print(the_Result2)
The filter function brings a function and even the iterable as arguments and makes the iterator from the iterable’s elements for which the function gets back to the right value. The str.isdigit technique returns “
True” if all characters in the string are digits, and there is a minimum of one character, otherwise “
False” is returned.
Hint: We fundamentally test if every character in the string is the digit, please return the outcome, and join the digits into the single string before taking the ‘
int()’ class to obtain the integer value.
5. Use the built-in ‘type()’ class
If you are not certain what type the class variable stores, please take the built-in ‘
integer5 = 1890 print(type(integer5)) print(isinstance(integer5, int)) string5 = '1902' print(type(string5)) print(isinstance(string5, str))
<class 'int'> True <class 'str'> True
The type class returns the object’s type. The isinstance function might return “True” if the passed-in object is the instance.
The “typeerror: unsupported operand type(s) for +: ‘int’ and ‘str’“ error is released during your attempt to add the integer into the string. Lastly, we hope that you can find it useful after reading this tutorial guide.