What does print(... sep='', '\t' ) mean?

krona picture krona · Mar 1, 2014 · Viewed 133.3k times · Source

I am having a bit of trouble trying to find an answer to this. I would like to know what the syntax sep="" and \t means. I have found some informaion about it but I didn't quite understand what the purpose of using the syntax was. I'm looking for an explanation of what it does and when / why you would use it.

An example of sep='' being used:

print('Property tax: $', format(tax, ',.2f'), sep='') 

Answer

Martijn Pieters picture Martijn Pieters · Mar 1, 2014

sep='' in the context of a function call sets the named argument sep to an empty string. See the print() function; sep is the separator used between multiple values when printing. The default is a space (sep=' '), this function call makes sure that there is no space between Property tax: $ and the formatted tax floating point value.

Compare the output of the following three print() calls to see the difference

>>> print('foo', 'bar')
foo bar
>>> print('foo', 'bar', sep='')
foobar
>>> print('foo', 'bar', sep=' -> ')
foo -> bar

All that changed is the sep argument value.

\t in a string literal is an escape sequence for tab character, horizontal whitespace, ASCII codepoint 9.

\t is easier to read and type than the actual tab character. See the table of recognized escape sequences for string literals.

Using a space or a \t tab as a print separator shows the difference:

>>> print('eggs', 'ham')
eggs ham
>>> print('eggs', 'ham', sep='\t')
eggs    ham