python - 如何在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
如何解决这个问题呢?
解决方案
", ".join(["Welcome to India"]*3)
推荐阅读
- java - 获取当前登录的用户 Spring
- reactjs - 我如何从 React DatePicker 获得 UTC +1?
- bash - 删除重复行立即重复忽略第一个字段
- python - df.iloc[[i]] 和 df.iloc[i] 有什么区别
- laravel - 带参数的重定向()->动作()不起作用
- c# - 创建用户 DateTime 类
- list - 使用给定列表的所有组合创建无限列表的 Haskell 方法
- html - iframe 中的 Pgadmin 界面
- python - 基于正态分布的 Python 绘图点
- ios - 如何在 UIKit ViewController 中使用 EnvironmentObject