本文介绍了print(... sep='', '\t' ) 是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

正在使用的 sep='' 示例:

print('房产税:$', format(tax, ',.2f'), sep='')
解决方案

sep='' 在函数调用的上下文中将命名参数 sep 设置为空细绳.请参阅print() 函数sep 是打印时在多个值之间使用的分隔符.默认是一个空格 (sep=' '),这个函数调用确保在 Property tax: $ 和格式化的 tax浮点值.

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

>>>打印('foo','bar')富吧>>>打印('foo','bar',sep='')食物吧>>>打印('foo','bar',sep=' -> ')富 ->酒吧

所有改变的是 sep 参数值.

\t 在字符串文字中制表符,水平空白,ASCII码点9.

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

使用空格或 \t 标签作为打印分隔符显示差异:

>>>打印('鸡蛋','火腿')鸡蛋火腿>>>打印('鸡蛋','火腿',sep='\t')鸡蛋火腿

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='') 
解决方案

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

这篇关于print(... sep='', '\t' ) 是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 14:08