How to concatenate strings in a list? [duplicate]

1

If I have a list of string objects and want to concatenate all these items, how do I do this?  For example:

row1 = ['___', '___', '___']

I would like to print: '___|___|___'

I've been able to do this, but it's not very practical: line1 = row1[0] + "|" + row1[1] + "|" + row1[2]

    
asked by anonymous 22.11.2018 / 12:47

1 answer

2

The string has a method called join who does the service:

line1 = '|'.join(row1)

It will concatenate all values in the list by adding the character between them. The join tends to be more efficient than the concatenation itself (1) and allows to inform different characters (2).

22.11.2018 / 12:51