PBE logo

Textwrap Module - Text wrapping and fillingΒΆ

wrap( text[, width[, ...]]) - Wraps the single paragraph in text (a string) so every line is at most width characters long. Returns a list of output lines, without final newlines.

wrap('"Bhutan" may be derived from the Sanskrit word Bhu-Utthan (highlands). In
another theory of Sanskritisation, Bhots-ant means "End of Tibet", as
Bhutan is immediately to Tibet south.')

['"Bhutan" may be derived from the Sanskrit word Bhu-Utthan (highlands).',
'In another theory of Sanskritisation, Bhots-ant means "End of Tibet",',
'as Bhutan is immediately to Tibet south.']

fill( text[, width[, ...]]) - Wraps the single paragraph in text, and returns a single string containing the wrapped paragraph. fill() is shorthand for “n”.join(wrap(text, ...))

print fill('"Bhutan" may be derived from the Sanskrit word Bhu-Utthan
(highlands). In another theory of Sanskritisation, Bhots-ant means "End of
Tibet", as Bhutan is immediately to Tibet south.')

"Bhutan" may be derived from the Sanskrit word Bhu-Utthan (highlands).
In another theory of Sanskritisation, Bhots-ant means "End of Tibet",
as Bhutan is immediately to Tibet south.

dedent( text) - Remove any common leading whitespace from every line in text.

s = '''\
hello
world
'''
print repr(s)            # prints '    hello\n      world\n    '
print repr(dedent(s))    # prints 'hello\n  world\n'