How to Solve Python TypeError: can’t multiply sequence by non-int of type ‘float’

by | Programming, Python

In Python, we can multiply a string by a numerical value, and this operation will duplicate the string by an amount equal to the numerical value. We can only use integers to multiply strings, not floating-point values. Floating points are decimal point numbers; you cannot have a fraction of a string in Python by multiplying; you would have to use slicing to get a fraction of a string. If you try to multiply a string by a floating-point value, you will raise the error: “TypeError: python can’t multiply sequence by non-int of type ‘float“.

This tutorial will go through the error in detail and solve it with several examples.


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

What is a TypeError?

TypeError occurs in Python when you perform an illegal operation for a specific data type. For example, suppose you try to index a floating-point number. In that case, you will raise the error: “TypeError: ‘float’ object is not subscriptable“ because floating-point values do not support the indexing operation.

Multiplying a String by an Integer

Python supports multiplying strings with numbers. Let’s look at an example of multiplying a string by an integer:

string = "research"

n = 4

print(string * n)

The above code defines a string with the value research and defines a variable n, which stores the integer value 4. The next step prints the product of the string by n. Let’s run the code to see what happens:

researchresearchresearchresearch

We can also multiple list and tuple objects by integer values. Doing so will duplicate the contents of the list or tuple, and the list length will increase depending on the value of the integer. Let’s look at an example of multiplying a list by an integer:

this_list = ["xlnet", "gpt3", "bert", "elmo"]

n = 3 

print(this_list * n) 

The above code defines a list that stores four strings and defines a variable n which holds the integer value 3. The following step prints the product of the list. Let’s run the code to see what happens:

['xlnet', 'gpt3', 'bert', 'elmo', 'xlnet', 'gpt3', 'bert', 'elmo', 'xlnet', 'gpt3', 'bert', 'elmo']

Let’s compare the length of the original list and the duplicated list using the len() function.

print(len(this_list)

print(len(this_list * n))
4 

12

The duplicate list is three times as long as the original list.

Multiplying a String by a Float

Python does not support multiplying strings by non-integer values, and this is the same as saying Python does not support using numbers to get fractions of a string. Let’s look at some example scenarios.

Example #1: Duplicating a String

Let’s try to multiply a string by a float value.

string = "research"

n = 4.6

print(string * n)

Instead of using an integer value of 4, we use a float value of 4.6. Let’s what happens when we try to run the code:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
1 print(string * n)

TypeError: can't multiply sequence by non-int of type 'float'

Solution

We can replace the float with an integer value to solve this error. First, we can use the int() function to convert a float to an integer. Let’s look at the revised code and the output:

string = "research"

n = int(4.6)

print(string * n)
researchresearchresearchresearch

The problem with using the int() function is it truncates the float value regardless of the decimal value. We can use the round() function to round the float value to the nearest integer. Let’s look at this version of the revised code.

string = "research"

n = int(round(4.6, 0))

print(string * n)

The round() function returns a float value of 5.0 and the int() function converts it to an integer. Then we can multiply the string by the integer value. Let’s run the code and see what happens:

researchresearchresearchresearchresearch

Example #2: Converting Numerical String To Float

There are scenarios where we have a string that contains a numerical value, and we want to multiply it by a floating-point value. Let’s look at an example:

string = "100"

n = 5.0

print(string * n)

Let’s run the code to see what happens:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
1 print(string * n)

TypeError: can't multiply sequence by non-int of type 'float'

We raise the error because we are trying to multiply a string by a float, even though the string is numerical.

Solution

To solve this problem, we can convert the string to a float. In Python, we can multiply floats by other floats. Let’s look at the revised code.

string = "100"

float_value = float(string)

n = 5.0

print(float_value * n)

The above code converts the string value to a floating-point number using the float() function. The following step prints the product of the two floating-point values to the console. Let’s look at the result:

500.0

Example #3: Taking Input from User and Multiplying by a Floating-Point Number

Let’s write a program that calculates the profit made from a bakery selling a certain amount of cakes at the end of the day. The program takes the number of cakes sold as input from the user using the input() function. We store the input in the variable number_of_cakes. We can sell a fraction of a cake, and therefore the user can input a decimal value. We can then assign the cake price to a variable named cake_price. We multiply the number_of_cakes by cake_price and store the value in a variable called profit. Let’s look at the code:

number_of_cakes = input("How many cakes were sold today? : ")

cake_price = 3.99

profit = number_of_cakes * cake_price

print(f'Profit made today: ${round(profit, 2)}')

The above code uses the round() function to round the profit value to two decimal places. Let’s run the code to see what happens.

How many cakes were sold today? : 40.5
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
1 profit = number_of_cakes * cake_price

TypeError: can't multiply sequence by non-int of type 'float'

Solution

To solve this error, we can convert the input to a floating-point value by wrapping the float() function around the input() function. Let’s look at the revised code.

number_of_cakes = float(input("How many cakes were sold today? : "))

cake_price = 3.99

profit = number_of_cakes * cake_price

print(f'Profit made today: ${round(profit, 2)}')

Let’s run the code to see what happens:

How many cakes were sold today? : 40.5

Profit made today: $161.59

Summary

Congratulations on reading to the end of this tutorial. The error “TypeError: can’t multiply sequence by non-int of type ‘float'” occurs when you try to multiply a string by a floating-point value. This operation is not possible because it would result in multiplying a string by fractional values. To solve this error, you can ensure that you a performing multiplication between a string and an integer. You can convert a float to an integer using the int() function and the round() function to get the nearest whole number from a floating-point value. If you are trying to multiply a numerical string, you can ensure you convert the string value to a floating-point number using the float() function before performing any calculations.

For further reading on TypeErrors involving string manipulation, go to the article: How to Solve Python TypeError: a bytes-like object is required, not ‘str’.

Go to the online courses page on Python to learn more about Python for data science and machine learning.

Have fun and happy researching!

Research Scientist at Moogsoft | + posts

Suf is a research scientist at Moogsoft, specializing in Natural Language Processing and Complex Networks. Previously he was a Postdoctoral Research Fellow in Data Science working on adaptations of cutting-edge physics analysis techniques to data-intensive problems in industry. In another life, he was an experimental particle physicist working on the ATLAS Experiment of the Large Hadron Collider. His passion is to share his experience as an academic moving into industry while continuing to pursue research. Find out more about the creator of the Research Scientist Pod here and sign up to the mailing list here!