👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
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 one string by another string, you will encounter the error " TypeError: unable to multiply sequence for non -int of type ’str’" error.
In this guide, we explain what this error means and where you might encounter it in your code. Let’s look at an example of this error to help you understand how to fix it.
TypeError: Cannot multiply sequence by non-int of type ’str’
Strings are a type of sequence . This is because they contain characters that Python can iterate over. Other types of sequences include tuples, dictionaries, and lists.
Use the multiplication operator (*) to create a string that repeats the contents of a string. Consider the following code:
This code returns: "Cakes! Cakes! ". The multiplication operator causes our string to repeat twice.
You cannot use the multiplication operator to multiply a string by a string. Integers and numbers to Floating point are the only values ‚Äã‚Äãthat can be multiplied by values ‚Äã‚Äãof the same data type. Python has no way of interpreting the multiplication of two strings.
An example scenario
Let’s create a program that calculates how much money a restaurant made from their jam scones on a Thursday afternoon.