Python 在字符串中插入制表符

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

Python insert tab in string

python

提问by cooldog

I just began learning Python. I'm not sure how to insert tab in string. For instance, how can i insert tab to the right of "World" in the strings below:

我刚开始学习 Python。我不确定如何在字符串中插入制表符。例如,如何在下面的字符串中在“世界”的右侧插入选项卡:

example = '    Hello World'

Everytime I use Tab after "World" a drop down menu appears, how can I avoid that? thanks! I tried using \t, but then example.lstrip()gives the result of Hello World\t, instead of Hello World

每次我在“世界”之后使用 Tab 时都会出现一个下拉菜单,我该如何避免?谢谢!我尝试使用\t, 但随后 example.lstrip()给出了Hello World\t, 而不是Hello World

Thanks for the answers. I used example = ' Hello World\t', it works. print(example.lstrip())gives the result: Hello World, without \t

感谢您的回答。我用过example = ' Hello World\t',有效。 print(example.lstrip())给出结果:Hello World, 没有\t

回答by Deca

The dropdown error is because of the editor you are using. Just try using plain notepad++. It's working! I tried.

下拉错误是因为您使用的编辑器。只需尝试使用普通记事本++。它正在工作!我试过。

print "Hello World\t",
print "hi"

Just to make sure that there is tab after the first print, print one more statement. you will come to visual it. Make sure to use comma ',' after print to stay in the same line if you are using python 2.7. Else use end="" for python 3+ to stay in the same line.

为了确保第一次打印后有制表符,再打印一条语句。你会看到它。如果您使用的是 python 2.7,请确保在打印后使用逗号 ',' 以保持在同一行。否则使用 end="" 对于 python 3+ 保持在同一行。