You can multiply two numbers together in Python. You can also multiply a number by a string. This returns a sequence of a string that repeats a specific number of times.
If you try to multiply a string by another string, you encounter the “TypeError: can’t multiply sequence by non-int of type ‘str’” error.
In this guide, we talk about what this error means and where you may encounter it in your code. We walk through an example of this error to help you figure out how to solve it.
TypeError: can’t multiply sequence by non-int of type ‘str’
Strings are a type of sequence. This is because they contain characters over which Python can iterate. Other types of sequences include tuples, dictionaries, and lists.
You use the multiplication operator (*) to create a string that repeats the contents of a string. Consider the following code:
print("Cakes! " * 2)
This code returns: “Cakes! Cakes!”. The multiplication operator makes our string repeat twice.
You cannot use the multiplication operator to multiply a string by a string. Integers and floating-point numbers are the only values that can be multiplied by values of the same data type. There is no way for Python to interpret multiplying two strings.
An Example Scenario
Let’s build a program that calculates how much money a restaurant has made on their jam scones on a Thursday afternoon.
To start, we need to define the price of a jam scone. We also ask the user to tell us how many jam scones they sold in their last day of business using an input() statement:
jam_scone = "1.95" sold = input("How many jam scones did you sell yesterday? ")
Next, we multiply these two values together. This will tell us how much money was earned from jam scones on a given day:
earned_from_scones = jam_scone * sold
Now that we know how much was earned from scones, we write a print()
statement which informs the user of the result of our calculation:
print("You earned ${} from selling scones.".format(earned_from_scones))
The format() method lets us add the value of “earned_from_scones” where our curly braces ({}) appear in our string.
We also need to calculate how much profit is made on each scone. We know that we make a 25-cent profit on each scone. To calculate how much profit is made, we use this code:
profit = sold * 0.25 print("You made a ${} profit from selling scones.".format(profit))
This code calculates how much in profit was made from each scone and then prints that value to the console. Let’s run our code and see if it works:
How many jam scones did you sell yesterday? 17 Traceback (most recent call last): File "main.py", line 4, in <module> earned_from_scones = jam_scone * sold TypeError: can't multiply sequence by non-int of type 'str'
Our code returns an error.
The Solution
Let’s analyze the line of code from our error:
earned_from_scones = jam_scone * sold
While this line of code looks fine, there is a problem: we are trying to multiply two string values together. We store “jam_scone” as a string. The input()
method returns a string which means that sold()
has a string value.
To solve this problem, we need to make sure that both “jam_scone” and “sold” are floating points. This lets us perform a mathematical operation on these values.
Next, we change how we declare the values of these variables:
jam_scone = 1.95 sold = float(input("How many jam scones did you sell yesterday? "))
The value of “jam_scone” is no longer in quotation marks. This shows we have changed “jam_scone” from a string to a float. We have also used the float() method to convert the value of “sold” to a floating-point number.
Run our code and see what happens:
How many jam scones did you sell yesterday? 17 You earned $33.15 from selling scones. You made a $4.25 profit from selling scones.
Our code works successfully. First, our code asks the user to insert how many scones are sold into the console. Next, our program calculates how much money the store earned from selling scones. Finally, our program calculates the profits generated from scone sales.
Conclusion
The “TypeError: can’t multiply sequence by non-int of type ‘str’” error occurs if you try to multiply two string values together. You can fix this problem by ensuring that you either multiply two numerical values together or that you only multiply a string by an integer.
Now you’re ready to solve this error like a Pythonista!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.