Python 3.x 无法在python中正确连接字符串
我不能正确地连接字符串 代码如下:Python 3.x 无法在python中正确连接字符串,python-3.x,string,list,Python 3.x,String,List,我不能正确地连接字符串 代码如下: def mub(s): count = 1 for i in range(len(s)): words = s[i] * count list_of = list(words) print('-'.join(list_of),end='') count+=1 mub('Abcd') 它将此作为输出: ab-bc-c-cd-d-d-d 但我想要的结果是: a-bb-ccc-dddd 我认为错误是在for循环之后。一
def mub(s):
count = 1
for i in range(len(s)):
words = s[i] * count
list_of = list(words)
print('-'.join(list_of),end='')
count+=1
mub('Abcd')
它将此作为输出:
ab-bc-c-cd-d-d-d
但我想要的结果是:
a-bb-ccc-dddd
我认为错误是在for循环之后。一个解决方案是保留您想要加入的字符串列表
-
。例如:
def mub(s):
count = 1
to_join = []
for i in range(len(s)):
to_join.append(s[i] * count)
count+=1
print('-'.join(to_join))
mub('abcd')
印刷品:
a-bb-ccc-dddd