首页 > 解决方案 > 如何在Python中分隔字符串乘以整数?

问题描述

假设我们有一个文本“欢迎来到印度”。我想将此字符串乘以 3,但要以逗号分隔,例如“欢迎来到印度,欢迎来到印度,欢迎来到印度”。问题是我知道这段代码可以工作:

a = 'Welcome to India'

required = a * 3 # but this code is not comma-delimited.

此外,这段代码也不能正常工作

required = (a + ", ") * 3 # because it puts comma even at the end of the string

如何解决这个问题呢?

标签: pythonstring

解决方案


", ".join(["Welcome to India"]*3)

推荐阅读