The join method

The join method merges the elements of a list into a string with the specified separator (space, comma, etc.).

Syntax

python'separator'.join(list of lines)

Example

Let's merge the list elements into a string:

pythonlst = ['ab', 'cd', 'ef']
txt = ' '.join(lst)
print(txt)

Result of code execution:

python'ab cd ef'

Example

Now let's use a comma as a separator:

pythonlst = ['ab', 'cd', 'ef']
txt = ','.join(lst)
print(txt)

Result of code execution:

python'ab,cd,ef'

See also

  • method split,
    which divides a string by a separator
  • method partition,
    which divides a string by the first match of a substring