You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python中字符串相乘报错:TypeError: can't multiply sequence by non-int of type 'str'

Python字符串相乘报错原因解析
  • Python中字符串的*运算符仅支持字符串与整数相乘,作用是将字符串重复指定整数次数,例如:

    print("Arsham" * 2)  # 输出: ArshamArsham
    
  • 你执行的"Arsham" * "Arsham"是两个字符串相乘,这违反了Python的语法规则:序列类型(字符串属于序列)的乘法运算要求右侧操作数必须是整数,而你传入的是字符串类型,因此解释器抛出TypeError: can't multiply sequence by non-int of type 'str'错误。

  • 另外,你期望的"Arsham" * "Arsham"这种输出在Python中没有对应的内置行为。如果想要拼接两个字符串,应该使用+运算符:

    print("Arsham" + "Arsham")  # 输出: ArshamArsham
    

内容的提问来源于stack exchange,提问作者Arsham Soltani

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 19:38:10