Für flexible Optional können Sie sich in der Standardbibliothek textwrap ansehen.
Beispiel:
>>> hamlet='''\
... To be, or not to be: that is the question:
... Whether 'tis nobler in the mind to suffer
... The slings and arrows of outrageous fortune,
... Or to take arms against a sea of troubles,
... And by opposing end them? To die: to sleep;
... No more; and by a sleep to say we end
... '''
>>> import textwrap
>>> wrapper=textwrap.TextWrapper(initial_indent='\t', subsequent_indent='\t'*2)
>>> print wrapper.fill(hamlet)
To be, or not to be: that is the question: Whether 'tis nobler in the
mind to suffer The slings and arrows of outrageous fortune, Or to
take arms against a sea of troubles, And by opposing end them? To
die: to sleep; No more; and by a sleep to say we end
Sie können sehen, dass Sie nicht nur leicht flexiblen Raum auf der Vorderseite jeder Zeile hinzufügen können, Sie jede Zeile trimmen passen, hyphenate, erweitern tabs usw.
Es wird wickeln (daher der Name) Linien, die zu lang geworden, weil der Zugaben auf der Vorderseite:
>>> wrapper=textwrap.TextWrapper(initial_indent='\t'*3,
... subsequent_indent='\t'*4, width=40)
>>> print wrapper.fill(hamlet)
To be, or not to be: that is the
question: Whether 'tis nobler in the
mind to suffer The slings and arrows
of outrageous fortune, Or to take
arms against a sea of troubles, And
by opposing end them? To die: to
sleep; No more; and by a sleep to
say we end
Sehr flexibel und nützlich.
bearbeiten
Wenn Sie die Bedeutung von Zeilenenden im Text mit TextWrap zu halten, kombiniert nur mit Teilungslinien TextWrap Linie zu halten die gleichen Endungen.
Beispiel hängender Einzug:
import textwrap
hamlet='''\
Hamlet: In the secret parts of Fortune? O, most true! She is a strumpet. What's the news?
Rosencrantz: None, my lord, but that the world's grown honest.
Hamlet: Then is doomsday near.'''
wrapper=textwrap.TextWrapper(initial_indent='\t'*1,
subsequent_indent='\t'*3,
width=30)
for para in hamlet.splitlines():
print wrapper.fill(para)
print
druckt
Hamlet: In the secret parts
of Fortune? O, most true!
She is a strumpet. What's
the news?
Rosencrantz: None, my lord,
but that the world's grown
honest.
Hamlet: Then is doomsday
near.
Ich wusste nicht, dass, danke! – JeromeJ