How To Resolve TypeError: Can’t Multiply Sequence By Non-Int Of Type ‘str’ In Python

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

In this article, I should show you the cause of the TypeError: can’t multiply sequence by non-int of type ‘str’ , and a solution to fix it, like converting the string to integer or float, as well as you can also use the type() or isinstance() function to check the variable’s data type. Post details below.

What causes the TypeError: can’t multiply sequence by non-int of type ‘str’?

The error happens because you multiply one string by another.

Example: 

myString = 'learnshareit'
numOfCopies = '2'
print(myString * numOfCopies)

Output:

Traceback (most recent call last):
 File "./prog.py", line 3, in <module>
TypeError: can't multiply sequence by non-int of type 'str'

How to solve the TypeError: can’t multiply sequence by non-int of type ‘str’?

Convert string to integer or float

Example:

  • Two variables contain strings. I use the int() function that converts those strings to integers.
  • Executing the multiplication will not throw an error.
myString = 'learnshareit '
numOfCopies = '2'

# Use the int() function to convert a string to an integer.
numOfCopies  = int(numOfCopies)
print('String after being multiplied: ', myString * numOfCopies)

Output:

String after being multiplied: learnshareit learnshareit

For variables containing only numeric strings, you can use int() and float() to convert the string to an integer or decimal so that the result is a product of numbers.

Example:

firstFactor = '10'
secondFactor = '2'

# Use the int() function to convert a string to an integer.
firstFactor  = int(firstFactor)
secondFactor = int(secondFactor)
print('Integer multiplication result:', firstFactor * secondFactor)

Output:

Integer multiplication result: 20
firstFactor = '10'
secondFactor = '2'

# Use the float() function to convert a string to an integer.
firstFactor  = float(firstFactor)
secondFactor = float(secondFactor)
print('Float multiplication result:', firstFactor * secondFactor)

Output:

Float multiplication result: 20.0

Use the type() function

Example:

  • Use the type() function to check the variable’s data type.
  • If the integer type is correct, multiply by the string.
myString = 'learnshareit '
numOfCopies = '2'

# Use the type() function to check the data type of the variable.
if type(numOfCopies) is int:
    print(myString * numOfCopies)
else:
    # Use the int() function to convert a string to an integer.
    numOfCopies = int(numOfCopies)
    print('String after multiplication:', myString * numOfCopies)

Output:

String after multiplication: learnshareit learnshareit

Or you can use the isinstance() function to check the variable’s data type.

Example:

myString = 'learnshareit '
numOfCopies = '2'

# Use the isinstance() function to check the data type of the variable.
if isinstance(numOfCopies, int) is True:
    print(myString * numOfCopies)
else:
    # Use the int() function to convert a string to an integer.
    numOfCopies = int(numOfCopies)
    print('String after multiplication:', myString * numOfCopies)  

Output:

String after multiplication: learnshareit learnshareit

The isinstance() function checks that the variable ‘numOfCopies’ is not an integer so that it will execute the ‘else’ statement.

Summary

The TypeError: can’t multiply sequence by non-int of type ‘str’ in Python is quite tricky, but you could fix it. Converting string to integer or float is the simplest way for you. Hopefully, through my article, you will have your solution. If there is another way, let us know in the comments below. Thanks for reading!

Maybe you are interested:

Leave a Reply

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