[Tutor] Print text position problems when using triple quotes

Michael Dunn misha.dunn at gmail.com
Thu Feb 24 21:48:17 CET 2005


Hi Luke,

> Is there a way to preserve the readability of the code and have
> printed text from indented blocks, say, nested conditionals, appear
> flush at left, not printed exactly where I've written them in the
> script?

you can use the textwrap module for this.

>>> from textwrap import dedent
>>> print dedent("""\
...     some
...     indented
...     text""")
some
indented
text
>>> 

"dedent" is a utility function of the textwrap module that trims every
line by an equal amount. The trick here is the backslash so that the
first line doesn't count. The "fill" and "wrap" functions of textwrap
might also interest you:
http://www.python.org/doc/2.3.5/lib/module-textwrap.html.

Cheers, Michael


More information about the Tutor mailing list