我有一个列表,它由不同的字符串(句子,单词......)组成,我想将它们连接在一起成为一个字符串。 我想:
kafka = ['Das ist ein schöner Tag.', '>>I would like some ice cream and a big cold orange juice!',...]
''.join(kafka) #but strings stay the same as before
我有一个列表,它由不同的字符串(句子,单词......)组成,我想将它们连接在一起成为一个字符串。 我想:
kafka = ['Das ist ein schöner Tag.', '>>I would like some ice cream and a big cold orange juice!',...]
''.join(kafka) #but strings stay the same as before
你需要做的:
kafka = ['Das ist ein schöner Tag.', '>>I would like some ice cream and a big cold orange juice!',...]
s = ''.join(kafka)
s
现在包含您的连接字符串。
基本上你是在正确的轨道上,但你需要将连接操作分配给一个新的变量或直接使用它。原始列表不会受到影响。
将它们连接起来有或没有任何空格之间:
no_spaces = ''.join(kafka)
with_spaces = ' '.join(kafka)
print no_spaces
print with_spaces
的原因是
''.join(kafka)
返回,而无需修改卡夫卡新的字符串。尝试:
my_string = ''.join(kafka)
如果你想句子之间的空间,然后使用:
my_string = ' '.join(kafka)
''.join(kafka)
返回一个字符串。为了使用它,它存储到一个变量:
joined = ''.join(kafka)
print joined
注意:您可能想用空格代替' '
加盟。
可以使用加入,
假设你的列表是
['Das ist ein schöner Tag.', '>>I would like some ice cream and a big cold orange juice!']
>>> kafka = ['Das ist ein schöner Tag.', '>>I would like some ice cream and a big cold orange juice!']
>>>' '.join(kafka)
输出:
'Das ist ein sch\xf6ner Tag. >>I would like some ice cream and a big cold orange juice!'
并没有分配的' ''。加入结果(卡夫卡)'的任何东西?如果它本身就在一条线上,它不会有任何影响。 – Kevin