25

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='') 
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
krona
  • 625
  • 3
  • 9
  • 13

4 Answers4

49

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
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • print('Property tax: $', format(tax, ',.2f'), sep='') would be an example of a line of code its used in. – krona Mar 01 '14 at 16:01
  • @krona: `sep=''` is not the same thing as `sep""`. See the [`print()` function documenation](http://docs.python.org/3/library/functions.html#print); the code sets the `sep` keyword argument to an empty string. – Martijn Pieters Mar 01 '14 at 16:02
  • @Noumenon: In Python 2, use `from __future__ import print_function`; this is not limited to just Python 3. The question was using `print()` as a function, hence my tailoring to that case. – Martijn Pieters Jan 12 '16 at 13:01
  • By 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. – Noumenon Jan 12 '16 at 13:10
  • 1
    @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. – Martijn Pieters Jan 12 '16 at 13:16
1

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
1

sep='\t' is often used for Tab-delimited file.

Riadh Saïd
  • 69
  • 1
  • 11
0

The sep='\t' can be use in many forms, for example if you want to read tab separated value: Example: I have a dataset tsv = tab separated value NOT comma separated value df = pd.read_csv('gapminder.tsv'). when you try to read this, it will give you an error because you have tab separated value not csv. so you need to give read csv a different parameter called sep='\t'.

Now you can read: df = pd.read_csv('gapminder.tsv, sep='\t'), with this you can read the it.