Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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='')  
like image 316
krona Avatar asked Mar 01 '14 15:03

krona


People also ask

What does '\ t mean in Python?

In Python strings, the backslash "\" is a special character, also called the "escape" character. It is used in representing certain whitespace characters: "\t" is a tab, "\n" is a newline, and "\r" is a carriage return.

What does Sep t mean in R?

sep="\t" tells R that the file is tab-delimited (use " " for space delimited and "," for comma delimited; use "," for a . csv file).

What does Sep do in print?

The sep parameter is primarily used to format the strings that need to be printed on the console and add a separator between strings to be printed.

What is sep =' in Python?

Sep is a parameter in python that primarily formats the printed statements in the output screen. Whitespace is the default value of this parameter. It adds a separator between strings to be printed.


2 Answers

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 
like image 141
Martijn Pieters Avatar answered Nov 15 '22 13:11

Martijn Pieters


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 
like image 29
Nazmul Hossain Avatar answered Nov 15 '22 13:11

Nazmul Hossain