center multi-line text in python output

大兔子大兔子 提交于 2021-02-18 18:06:02

问题


OK, so this seems like a really basic question, but I can't find a workable answer anywhere, so here goes.

I have some text:

text = '''
Come and see the violence inherent in the system. Help! Help! I'm being 
repressed! Listen, strange women lyin' in ponds distributin' swords is no 
basis for a system of government. Supreme executive power derives from a 
mandate from the masses, not from some farcical aquatic ceremony. The Lady 
of the Lake, her arm clad in the purest shimmering samite held aloft 
Excalibur from the bosom of the water, signifying by divine providence that 
I, Arthur, was to carry Excalibur. THAT is why I am your king.'''

It doesn't contain any line breaks or other formatting. I want to wrap my text so it displays properly in my ipython output window when I run my code. I also want it centered, and a bit shorter than the full window width (80 chars)

If I had a short text string (shorter than the line length) I could simply calculate the length of the string and pad it with spaces to get it to centre, or use the text.center() property to display it correctly.

If I had a text string that I just wanted to wrap, I could use:

from textwrap import fill
print(fill(text, width=50))

and set width to whatever

So I would have thought I could simply then:

from textwrap import fill
wrapped_text = (fill(text, width=50))
print(wrapped_text.center(80))

but it doesn't work. Everything is still left justified.

I'm sure I can't be the only person who has tried to do this. Can anybody help me out?


回答1:


The problem is that center expects a single-line string, and fill returns a multiline string.

The answer is to center each line, before joining them up.

If you look at the docs for fill, it's shorthand for:

"\n".join(wrap(text, ...))

So, you can just skip that shorthand and use wrap directly. For example, you can write your own function that does exactly what you want:

def center_wrap(text, cwidth=80, **kw):
    lines = textwrap.wrap(text, **kw)
    return "\n".join(line.center(cwidth) for line in lines)

print(center_wrap(text, cwidth=80, width=50))

Although if you're only doing this in one place, to immediately print it out, it's probably simpler to not even bother joining it:

for line in textwrap.wrap(text, width=50):
    print(line.center(80))



回答2:


wrapped_text is a list of strings so loop over the strings and center them.

import textwrap

text = "Come and see the violence inherent in the system. Help! Help! I'm being repressed! Listen, strange women lyin' in ponds distributin' swords is no basis for a system of government. Supreme executive power derives from a mandate from the masses, not from some farcical aquatic ceremony. The Lady of the Lake, her arm clad in the purest shimmering samite held aloft Excalibur from the bosom of the water, signifying by divine providence that I, Arthur, was to carry Excalibur. THAT is why I am your king."    

wrapped_text = textwrap.wrap(text)
for line in wrapped_text:
    print(line.center(80))


来源:https://stackoverflow.com/questions/50281190/center-multi-line-text-in-python-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!