Formattare testo multinea in Python

Mattepuffo's logo
Formattare testo multinea in Python

Formattare testo multinea in Python

Vi può essere capitato di usare un testo multinea in Python, e di aver messo degli spazi per rendere il codice più leggibile.

Solo che se lo stampate così com'è, avrete la stessa indentazione.

Per fare un pò di pulizia possiamo usare il modulo textwrap.

Ecco un esempio:

import textwrap

testo = """
  Questo è un testo
  multilinea
"""

print(testo)
print(textwrap.dedent(testo).strip())

In console:

  Questo è un testo
  multilinea

Questo è un testo
multilinea

Enjoy!


Condividi

1 Commenti

  • Wilma

    16 Facebook Pages That You Must Follow For Tassimo Coffee Machine Marketers Bosch Tassimo Machine

    17/07/2025

Commentami!