打印字符串,其中字符用“+”分隔,字符串本身用“,”分隔

时间:2013-03-08 23:27:04

标签: python python-2.7

我有一些变数:

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

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

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

因此,在每个字符串值中,字符由+分隔,字符串本身由,分隔。

如何实现这一目标?

1 个答案:

答案 0 :(得分:3)

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'