TypeError: can't multiply sequence by non-int of type 'str' i get this when i want to multiplied my name by my name in python

huangapple go评论121阅读模式
英文:

TypeError: can't multiply sequence by non-int of type 'str' i get this when i want to multiplied my name by my name in python

问题

当我想要打印“Arsham”乘以“Arsham”时,在我的终端中出现了这个错误,有人可以告诉我为什么吗?

我尝试打印我的名字乘以我的名字,我期望我的程序显示"Arsham" * "Arsham"

英文:

When I want to print "Arsham" multiplied to "Arsham", I get this Error in my terminal can someone tell me why?

I Try'd to print my name multiplied by my name, and I expecting to my program show me "Arsham" \* "Arsham"

答案1

得分: 1

你可以使用f-string来实现这个目的:

name = "Arsham"

print(f'"{name}" \\* "{name}"')

输出结果:

"Arsham" \* "Arsham"

或者... 使用join函数:

name = '"Arsham"'

print(' \\* '.join([name]*2))
英文:

You will find an f-string useful for this:

name = "Arsham"

print(f'"{name}" \\* "{name}"')

Output:

"Arsham" \* "Arsham"

Or... using join:

name = '"Arsham"'

print(' \\* '.join([name]*2))

答案2

得分: -1

字符串不能与字符串相乘。在Python中,字符串只能与整数相乘。

print("Arsham"*2)  # 输出为 ArshamArsham

如果你的输出应该是 "Arsham" \* "Arsham",那么可以使用以下代码:

print("Arsham", "*", "Arsham")
英文:

Strings can't multiplied to strings. In Python, Strings can multiplied only to integers.

print("Arsham"*2)  # output is ArshamArsham

If your output should be "Arsham" \* "Arsham" than this code will help you

print("Arsham", "\\*", "Arsham")

huangapple
  • 本文由 发表于 2023年8月9日 16:06:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76865751.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定