Python

textwrap dedent + fill (Multi-line Strings)

admin by @admin ADMIN
1d ago
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
`textwrap.dedent` strips common leading whitespace from a triple-quoted string — finally, you can indent multiline strings inside functions without breaking the formatting. `fill` wraps to a max width.
Python
Raw
import textwrap

def help_text() -> str:
    return textwrap.dedent("""\
        myapp 1.0
        =========

        Usage:
          myapp [-v|--verbose] command [options]

        Commands:
          run    — start the server
          stop   — graceful shutdown
    """)

print(help_text())

# Wrap a long paragraph to 60 cols
para = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt."
print(textwrap.fill(para, width=60))
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.