在 python 中使用缩进打印
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18756510/
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
Printing with indentation in python
提问by Algorithmatic
is there a way to print the following,
有没有办法打印以下内容,
print user + ":\t\t" + message
so that lengthy messages that are wider than the length of the terminal always wraps (starts from the same position) ? so for example this
以便比终端长度更宽的冗长消息总是换行(从同一位置开始)?所以例如这个
Username: LEFTLEFTLEFTLEFTLEFTLEFTLEFT
RIGHTRIGHTRIGHT
should become
应该成为
Username: LEFTLEFTLEFTLEFTLEFTLEFTLEFT
RIGHTRIGHTRIGHT
采纳答案by abarnert
I think what you're looking for here is the textwrap
module:
我认为您在这里寻找的是textwrap
模块:
user = "Username"
prefix = user + ": "
preferredWidth = 70
wrapper = textwrap.TextWrapper(initial_indent=prefix, width=preferredWidth,
subsequent_indent=' '*len(prefix))
message = "LEFTLEFTLEFTLEFTLEFTLEFTLEFT RIGHTRIGHTRIGHT " * 3
print wrapper.fill(message)
This prints:
这打印:
Username: LEFTLEFTLEFTLEFTLEFTLEFTLEFT RIGHTRIGHTRIGHT
LEFTLEFTLEFTLEFTLEFTLEFTLEFT RIGHTRIGHTRIGHT
LEFTLEFTLEFTLEFTLEFTLEFTLEFT RIGHTRIGHTRIGHT
If you actually want to use tabs in the indent, that's a little trickier, because you have to first tab-expand the initial_indent
to figure out the correct subsequent_indent
to use. And, because your prefix actually endswith two tabs, it's even more complicated. Here's the simplest I've come up with:
如果您真的想在缩进中使用制表符,那就有点棘手了,因为您必须首先对 进行制表符扩展initial_indent
以找出正确subsequent_indent
的使用方式。而且,因为您的前缀实际上以两个选项卡结尾,所以它更加复杂。这是我想出的最简单的:
user = "Username"
prefix = user + ":\t\t"
expanded_indent = textwrap.fill(prefix+'$', replace_whitespace=False)[:-1]
subsequent_indent = ' ' * len(expanded_indent)
wrapper = textwrap.TextWrapper(initial_indent=prefix,
subsequent_indent=subsequent_indent)
message = "LEFTLEFTLEFTLEFTLEFTLEFTLEFT RIGHTRIGHTRIGHT " * 3
print wrapper.fill(message)
If you do this repeatedly, you will probably want to wrap that mess up in a function.
如果您重复执行此操作,您可能希望将这些乱七八糟的东西包装在一个函数中。
回答by Algorithmatic
You can use str.ljust()
to pad out each line to the required width like so:
您可以使用str.ljust()
将每一行填充到所需的宽度,如下所示:
line_width = 20
print "Username:".ljust(line_width) + "LEFT"*6
print "".ljust(line_width) + "RIGHT"*3
The argument you pass to ljust
is the length you wish the string to be, as long as this is consistant the lines should line up correctly.
您传递给的参数ljust
是您希望字符串的长度,只要这是一致的,线条就应该正确排列。
Alternately, you can use string multiplication on lines where you just need the paddding like so:
或者,您可以在只需要填充的行上使用字符串乘法,如下所示:
print " "*line_width + "RIGHT"*3
This will have the exact same output as the last line in the above code.
这将具有与上述代码中的最后一行完全相同的输出。
回答by ntg
I suggest using formatto indent left e.g.:
我建议使用格式向左缩进,例如:
print 'Username: {:>40}'.format('Foo')
print ' {:>40}'.format('FooBar')
Will result to:
将导致:
Username: Foo
FooBar
Also,
还,
print '{:<30}{:<40}'.format('UserName:','Foo')
print '{:<30}{:<40}'.format('User:','FooBar')
print '{:<30}{:<40}'.format('','FooBar42')
will result to:
将导致:
UserName: Foo
User: FooBar
FooBar42
And so on...
等等...