英文:
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")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论