How To Transform Elements Of A List Into One String?
community, I have a list, that consists of different strings (sentences, words...) I would like to join them all together to one string. I tried: kafka = ['Das ist ein schöner Tag
Solution 1:
You need to do:
kafka = ['Das ist ein schöner Tag.', '>>I would like some ice cream and a big cold orange juice!',...]
s = ''.join(kafka)
s
now contains your concatenated string.
Solution 2:
Basically you are on the right track, but you need to assign the join operation to a new variable or use it directly. the original list won't be affected.
concatenate them with or without any spaces in between:
no_spaces = ''.join(kafka)
with_spaces = ' '.join(kafka)
print no_spaces
print with_spaces
Solution 3:
The reason is that
''.join(kafka)
returns the new string without modifying kafka. Try:
my_string = ''.join(kafka)
And if you want spaces between sentences then use:
my_string = ' '.join(kafka)
Solution 4:
''.join(kafka)
returns a string. To make use of it, store it into a variable:
joined = ''.join(kafka)
print joined
Note: you may want to join with a space ' '
instead.
Solution 5:
You can join it using ,
Assuming your list is
['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)
Output :
'Das ist ein sch\xf6ner Tag. >>I would like some ice cream and a big cold orange juice!'
Post a Comment for "How To Transform Elements Of A List Into One String?"