Search code examples
pythonpython-3.xprintingargument-unpacking

Display list items with custom separator using print() function


Can someone explain to me why when I'm trying to pass unpacked data into print function using asterisk, the optional argument "end" is applied only for the last list's element, and for the rest is default (space)

l = ['a', 'b', 'c']
print(*l, end='-')

I expected a-b-c- instead of a b c-


Solution

  • According to the print() docs,

    Print objects to the text stream file, separated by sep and followed by end.

    All non-keyword arguments are converted to strings like str() does and written to the stream, separated by sep and followed by end.

    The sep argument is used to control the separator between arguments to print. end just controls the line terminator.

    l = ['a', 'b', 'c']
    print(*l, sep='-', end='-')
    # a-b-c-