TypeError: can't multiply sequence by non-int of type 'str'
Exception: TypeErrorCategory: runtimeRaised at: the * operator, when one side is a sequence and the other is a string
What this error means
You used * between a sequence (string, list or tuple) and a string. Python gives * a second meaning for sequences â repeat â so "ab" * 3 is 'ababab'. That second meaning only accepts an integer repeat count. Two strings on either side of * satisfy neither rule, hence the deliberately wordy message.
Read it backwards: can't multiply sequence (the left side is a str, list or tuple) by non-int of type 'str' (the right side is a string, and only an int would do).
Cause 1 â input() always returns a string
This is the cause in almost every beginner report. input() hands back text, even when the user typed digits. "120" is a one-character-per-digit string, not the number 120:
price = input("Price: ") # user types 120 -> the string '120'
qty = input("Qty: ") # user types 3 -> the string '3'
print(price * qty) # TypeError
Traceback (most recent call last):
File "demo.py", line 4, in <module>
print(price * qty)
~~~~~~^~~~~
TypeError: can't multiply sequence by non-int of type 'str'
Convert at the boundary, the moment the text arrives:
price = float(input("Price: ")) # 120.0
qty = int(input("Qty: ")) # 3
print(price * qty) # 360.0
Cause 2 â data that arrived as text
The same trap appears everywhere text becomes data: a CSV field, a URL query parameter, sys.argv, an environment variable, a JSON string value, or a database column typed as text.
import csv, os
with open("scores.csv", encoding="utf-8") as f:
for row in csv.reader(f):
name, marks = row[0], int(row[1]) # csv gives strings
limit = int(os.environ.get("LIMIT", "10")) # env vars are strings
total = marks * limit
Cause 3 â one side converted, the other forgotten
A half-converted expression is easy to miss because it looks almost right. Note also what does work, so the difference is clear:
"5" * 3 # '555' â legal: repeat the string 3 times
"5" * "3" # TypeError â this error
5 * 3 # 15 â legal: arithmetic
[0] * 3 # [0, 0, 0] â legal: repeat the list
[0] * "3" # TypeError â same error, list on the left
How to fix it
- Convert at the point of entry, not at the point of use:
int(input())orfloat(input()). Every later line then works with real numbers. - Pick the right converter â
int()for whole counts,float()for money and measurements.int("12.5")raisesValueError, so usefloat()first if the text may contain a decimal point. - Wrap the conversion, not the whole program: a
try/except ValueErroraround theint()lets you re-prompt instead of crashing. - Print
repr(value)while debugging â quotes mean it is still a string.type(value)says the same thing more formally. - Strip stray characters before converting:
int(text.strip())tolerates the spaces and newlines thatinput()and file reads leave behind.
while True:
raw = input("Qty: ").strip()
try:
qty = int(raw)
break
except ValueError:
print(f"'{raw}' ek number nahi hai, dobara likho.")
Fix it interactively
These free problems are the same type-confusion bug â repair them in the browser:
Practice more Python bugs âRelated messages worth knowing
- can only concatenate str (not "int") to str â the same type mismatch on the + operator.
- ValueError: invalid literal for int() â what you get next when the text is not a number at all.
- unsupported operand type(s) for +: 'int' and 'str' â the third wording of the same underlying mistake.
- input() in Hindi â input() se number kaise lein â ā¤ĒāĨā¤°ā¤ž Hindi guide.
đŽđŗ Hindi ā¤ŽāĨā¤ ā¤¸ā¤Žā¤āĨā¤
TypeError: can't multiply sequence by non-int of type 'str' ka matlab hai aapne string ko string se multiply kiya. Wajah lagbhag hamesha yahi hoti hai ki input() hamesha string deta hai â user ne 120 type kiya, par mila '120'. Isliye price * qty me dono text hote hain. Python me * ka doosra matlab 'repeat' hota hai ("ab" * 3 = 'ababab'), aur wo sirf integer accept karta hai. Fix: conversion entry point par karo â price = float(input("Price: ")) aur qty = int(input("Qty: ")). Yaad rakho int("12.5") ValueError deta hai, isliye decimal ke liye float() use karo. Debug karte waqt repr(value) print karo â quotes dikhe to wo abhi bhi string hai.