Python print(... sep='', '\t' ) 是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22116482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:16:17  来源:igfitidea点击:

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

pythonpython-3.xsyntaxseparator

提问by krona

I am having a bit of trouble trying to find an answer to this. I would like to know what the syntax sep=""and \tmeans. 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.

我在试图找到答案时遇到了一些麻烦。我想知道语法sep=""\t含义。我找到了一些关于它的信息,但我不太明白使用语法的目的是什么。我正在寻找有关它的作用以及何时/为什么使用它的解释。

An example of sep=''being used:

一个sep=''被使用的例子:

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

采纳答案by Martijn Pieters

sep=''in the context of a function call sets the named argument septo an empty string. See the print()function; sepis 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 taxfloating point value.

sep=''在函数调用的上下文中,将命名参数sep设置为空字符串。查看print()功能sep是打印时在多个值之间使用的分隔符。默认为空格 ( sep=' '),此函数调用确保Property tax: $格式化的tax浮点值之间没有空格。

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

比较下面三个print()调用的输出,看看有什么不同

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

All that changed is the separgument value.

所有改变的是sep参数值。

\tin a string literalis an escape sequence for tab character, horizontal whitespace, ASCII codepoint 9.

\t在字符串文字中制表符、水平空白、ASCII 代码点 9的转义序列。

\tis easier to read and type than the actual tab character. See the table of recognized escape sequencesfor string literals.

\t比实际的制表符更易于阅读和输入。请参阅已识别的字符串文字转义序列表

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

使用空格或\t制表符作为打印分隔符显示了不同之处:

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

回答by Nazmul Hossain

sep=''ignore whiteSpace. see the code to understand.Without sep=''

sep=''忽略空格。看代码就明白了。没有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.

usingsep=''代码和输出。

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

回答by Riadh Sa?d

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

sep='\t'通常用于制表符分隔的文件。