0

我有一些变量:

A1 = 'abc'
B1 = 'def'
C1 = 'ghi'

我想编写以以下格式打印 A1、B1、C1 的代码:

a+b+c, d+e+f, g+h+i

所以在每个字符串值中,字符由 a 分隔,+字符串本身由 a 分隔,

如何实现?

4

1 回答 1

2
In [11]: str.join?
Namespace:  Python builtin
Docstring:
S.join(iterable) -> string

Return a string which is the concatenation of the strings in the
iterable.  The separator between elements is S.

 

In [12]: ", ".join("+".join(chars) for chars in [A1, B1, C1])
Out[12]: 'a+b+c, d+e+f, g+h+i'
于 2013-03-08T23:28:44.487 回答