[Solved] Python can’t Multiply Sequence by non-int of type ‘str’

The TypeError: can’t multiply sequence by non-int of type ‘str’ occurs if we multiply a string by another string without converting into an integer or floating-point.

In this tutorial, we will learn what exactly TypeError: can’t multiply sequence by non-int of type ‘str’ error means and how to resolve this TypeError in your program with examples.

TypeError: can’t multiply sequence by non-int of type ‘str’

Python is one of the best programming languages because of its features and simplicity. One such fantastic feature in Python is we can multiply strings with numbers.

Multiplying string with an integer

Let’s take an example to demonstrate multiplying string with numbers.

The other popular programming languages will never let you to multiple strings and integers. However, we can perform a multiplication between string and integer in Python. After the multiplication, the string is repeated for n times, as shown below.

text = "ItsMyCode"
n = 3
print(text*n)

Output

ItsMyCodeItsMyCodeItsMyCode

Here the string “ItsMyCode” is repeated multiplied by three and repeated three times in the output.

If we try to multiply the string with another string then the Python interpreter will throw TypeError: can’t multiply sequence by non-int of type ‘str’.

Multiplying string with another string

You cannot multiply a string with a non-integer value in Python. If we multiply a string with another string without converting to integer or floating point, we get an error can’t multiply sequence by non-int of type ‘str’.

Let us take a simple example of multiplying two numbers.

num1 ='5'
num2 ='6'
output =num1 * num2
print(output)

Output

Traceback (most recent call last):
  File "C:\Personal\IJS\Code\main.py", line 3, in <module>
    output =num1 * num2
TypeError: can't multiply sequence by non-int of type 'str'

Even though the number entered here is equal to the integer values, the Python interpreter will still consider it as a string and raise a TypeError.

The simplest way to resolve this issue is by converting both the strings into an integer and then performing a multiplication operation, as shown below.

num1 ='5'
num2 ='6'
output = int(num1) * int(num2)
print(output)

Output

30

Solution TypeError: can’t multiply sequence by non-int of type ‘str’

Now we know that TypeError: can’t multiply sequence by non-int of type str is caused by multiplying a string with another string. Let us see how we can resolve this error with an example.

Usually, we get this error when we receive input from the user, and the input() method alway returns the data in a string format.

Consider we have to calculate the total tax amount paid based on the distance traveled and the fare price.

In the above example, we are reading the fare price and distance data as user input, and we are calculating the tax amount by multiplying the (fare_price * distance)*(tax_percentage/100)

Even though the user inputs valid data, the input() method returns a string, meaning that fare_price and distance values are in string format. Hence we end up multiplying two strings resulting in the TypeError: can’t multiply sequence by non-int of type ‘str’.

tax_percentage = 5
fare_price = input("Please enter the fare amount charged  ")
distance = input("Please enter the distance travelled to calculate the total fare -  ")
total_fare = (fare_price * distance) * (5/100)
print(total_fare)

Output

Please enter the fare amount charged  100
Please enter the distance travelled to calculate the total fare -  5
Traceback (most recent call last):
  File "C:\Personal\IJS\Code\main.py", line 4, in <module>
    total_fare = (fare_price * distance) * 0.05
TypeError: can't multiply sequence by non-int of type 'str'

The best way to resolve this error is to convert the user input string to a floating-point using the float() method. 

This allows us to multiply the order_value and discount because both are floating-point numbers.

tax_percentage = 5
fare_price = float(input("Please enter the fare amount charged  "))
distance = float(input("Please enter the distance travelled to calculate the total fare -  "))
total_fare = (fare_price * distance) * (5/100)
print(total_fare)

Output

Please enter the fare amount charged  250.50
Please enter the distance travelled to calculate the total fare -  3.4
42.585
Note: We cannot multiply strings with floating-point numbers,if we do Python interpreter will throw TypeError: can't multiply sequence by non-int of type 'float'. Hence in the above example we have to convert both the inputs into floating-point and then perform multiplication.

Conclusion

We cannot multiply strings with any non integers values such as float, string etc. If we multiply a string with another string without converting into an integer we get TypeError: can’t multiply sequence by non-int of type 'str'

In order to solve this issue, TypeError: can’t multiply sequence by non-int of type ‘str’ ensure that either you are performing a multiplication between string and integer or alternatively you can convert all the string values into a floating-point number before performing any calculations. 

Leave a Reply

Your email address will not be published. Required fields are marked *

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like