如何在 Python 中编写“选项卡”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4488570/
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-18 15:58:53  来源:igfitidea点击:

How do I write a "tab" in Python?

pythontabs

提问by TIMEX

Let's say I have a file. How do I write "hello" TAB "alex"?

假设我有一个文件。我如何写“你好”标签“亚历克斯”?

采纳答案by Simone

This is the code:

这是代码:

f = open(filename, 'w')
f.write("hello\talex")

The \tinside the string is the escape sequence for the horizontal tabulation.

\t里面的字符串是水平制表转义序列。

回答by Knio

You can use \t in a string literal:

您可以在字符串文字中使用 \t:

"hello\talex"

"hello\talex"

回答by Darbio

It's usually \tin command-line interfaces, which will convert the char \tinto the whitespace tab character.

它通常\t在命令行界面中,它将字符\t转换为空白制表符。

For example, hello\talex-> hello--->alex.

例如,hello\talex-> hello--->alex

回答by CodeCupboard

The Python reference manualincludes several string literals that can be used in a string. These special sequences of characters are replaced by the intended meaning of the escape sequence.

Python参考手册包括几个可以在字符串中使用的字符串文字。这些特殊的字符序列被转义序列的预期含义替换。

Here is a table of some of the more useful escape sequences and a description of the output from them.

以下是一些更有用的转义序列及其输出描述的表格。

Escape Sequence       Meaning
\t                    Tab
\                    Inserts a back slash (\)
\'                    Inserts a single quote (')
\"                    Inserts a double quote (")
\n                    Inserts a ASCII Linefeed (a new line)

Basic Example

基本示例

If i wanted to print some data points separated by a tab space I could print this string.

如果我想打印一些由制表符空间分隔的数据点,我可以打印这个字符串。

DataString = "0\t12\t24"
print (DataString)

Returns

退货

0    12    24

Example for Lists

列表示例

Here is another example where we are printing the items of list and we want to sperate the items by a TAB.

这是另一个示例,我们正在打印列表中的项目,并且我们希望通过 TAB 来分散这些项目。

DataPoints = [0,12,24]
print (str(DataPoints[0]) + "\t" + str(DataPoints[1]) + "\t" + str(DataPoints[2]))

Returns

退货

0    12    24

Raw Strings

原始字符串

Note that raw strings (a string which include a prefix "r"), string literals will be ignored. This allows these special sequences of characters to be included in strings without being changed.

请注意,原始字符串(包含前缀“r”的字符串)、字符串文字将被忽略。这允许将这些特殊的字符序列包含在字符串中而无需更改。

DataString = r"0\t12\t24"
print (DataString)

Returns

退货

0\t12\t24

Which maybe an undesired output

这可能是不需要的输出

String Lengths

字符串长度

It should also be noted that string literals are only one character in length.

还应该注意的是,字符串文字的长度只有一个字符。

DataString = "0\t12\t24"
print (len(DataString))

Returns

退货

7

The raw string has a length of 9.

原始字符串的长度为 9。

回答by user1767754

As it wasn't mentioned in any answers, just in case you want to align and space your text, you can use the string format features. (above python 2.5) Of course \tis actually a TAB token whereas the described method generates spaces.

由于在任何答案中都没有提到,以防万一您想对齐和分隔文本,您可以使用字符串格式功能。(python 2.5 以上)当然\t实际上是一个 TAB 标记,而所描述的方法生成空格。

Example:

例子:

print "{0:30} {1}".format("hi", "yes")
> hi                             yes

Another Example, left aligned:

另一个例子,左对齐:

print("{0:<10} {1:<10} {2:<10}".format(1.0, 2.2, 4.4))
>1.0        2.2        4.4