krona krona - 12 days ago 6
Python Question

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

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
mean. I have found a little bit of info about it but I didn't quite understand what the purpose of using the syntax was. 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

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