What does print(... sep='', '\t' ) mean?
Solution 1
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
Solution 2
sep=''
ignore whiteSpace.
see the code to understand.Without sep=''
from itertools import permutations
s,k = input().split()
for i in list(permutations(sorted(s), int(k))):
print(*i)
output:
HACK 2
A C
A H
A K
C A
C H
C K
H A
H C
H K
K A
K C
K H
using sep=''
The code and output.
from itertools import permutations
s,k = input().split()
for i in list(permutations(sorted(s), int(k))):
print(*i,sep='')
output:
HACK 2
AC
AH
AK
CA
CH
CK
HA
HC
HK
KA
KC
KH
Related videos on Youtube
![krona](https://i.stack.imgur.com/5PnlM.jpg?s=256&g=1)
krona
Updated on July 11, 2022Comments
-
krona almost 2 years
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='')
-
krona over 10 yearsprint('Property tax: $', format(tax, ',.2f'), sep='') would be an example of a line of code its used in.
-
Martijn Pieters over 10 years@krona:
sep=''
is not the same thing assep""
. See theprint()
function documenation; the code sets thesep
keyword argument to an empty string. -
Martijn Pieters over 8 years@Noumenon: In Python 2, use
from __future__ import print_function
; this is not limited to just Python 3. The question was usingprint()
as a function, hence my tailoring to that case. -
Noumenon over 8 yearsBy the same logic any Python 2 answer is also good for Python 3 because 2to3 exists. You're causing confusion for Python 2 users by rejecting the edit. I will tag the question as Python 3, but no one ever notices that.
-
Martijn Pieters over 8 years@Noumenon: there is always going to be some confusion between the two versions. I do link to the function documentation, I call it a function and not a statement, etc.