
In Python, per avvolgere o troncare una stringa di una data larghezza (= numero di caratteri), utilizzare il modulo textwrap della libreria standard.
In questo articolo vengono descritti i seguenti contenuti.
- Avvolgi una stringa:
wrap(), fill()
- Tronca una stringa:
shorten()
- Testo Oggetto Involucro
Se si desidera scrivere stringhe lunghe su più righe nel codice anziché nell’output, vedere l’articolo seguente.
Il modulo pprint è utile per la formattazione e l’output di liste e dizionari. Vedi il seguente articolo.
Avvolgi una stringa:wrap(), fill()
Con la funzione wrap() del modulo textwrap, puoi ottenere un elenco diviso per interruzioni di parole per adattarsi a un determinato numero di caratteri.
Specificare il numero di caratteri per la larghezza del secondo argomento. Il valore predefinito è larghezza=70.
import textwrap
s = "Python can be easy to pick up whether you're a first time programmer or you're experienced with other languages"
s_wrap_list = textwrap.wrap(s, 40)
print(s_wrap_list)
# ['Python can be easy to pick up whether', "you're a first time programmer or you're", 'experienced with other languages']
Usa ‘n’.join(list) per ottenere la stringa con il codice di avanzamento riga n.
print('n'.join(s_wrap_list))
# Python can be easy to pick up whether
# you're a first time programmer or you're
# experienced with other languages
La funzione fill() richiede una stringa con interruzioni di riga, non un elenco. È lo stesso di ‘n’.join(list) dopo wrap() come nell’esempio sopra.
Ciò è quando utile non è necessario un elenco e si desidera generare una stringa a larghezza fissa.
print(textwrap.fill(s, 40))
# Python can be easy to pick up whether
# you're a first time programmer or you're
# experienced with other languages
Se viene specificato l’argomento max_line, le righe seguenti vengono omesse.
print(textwrap.wrap(s, 40, max_lines=2))
# ['Python can be easy to pick up whether', "you're a first time programmer or [...]"]
print(textwrap.fill(s, 40, max_lines=2))
# Python can be easy to pick up whether
# you're a first time programmer or [...]
Se omesso, per impostazione predefinita, alla fine viene emesso ‘ […]’. Può essere sostituito da qualsiasi stringa con l’argomento segnato.
print(textwrap.fill(s, 40, max_lines=2, placeholder=' ~'))
# Python can be easy to pick up whether
# you're a first time programmer or ~
L’argomento initial_indent può essere utilizzato per specificare una stringa da aggiungere all’inizio della prima riga. Viene quando viene utilizzato si desidera indentare l’inizio di un paragrafo.
print(textwrap.fill(s, 40, max_lines=2, placeholder=' ~', initial_indent=' '))
# Python can be easy to pick up whether
# you're a first time programmer or ~
Per altre impostazioni dettagliate, fare riferimento alla documentazione.
Tronca una stringa:shorten()
Se vuoi accorciare una stringa troncandola, usa la funzione shorten() del modulo textwrap.
Può essere troncato parola per parola per adattarsi a un determinato numero di caratteri con il segnaposto. Il segnaposto è ‘ […]’, per argomento predefinito, e può essere impostato con l’argomento segnaposto.
s = 'Python is powerful'
print(textwrap.shorten(s, 12))
# Python [...]
print(textwrap.shorten(s, 12, placeholder=' ~'))
# Python is ~
Testo Oggetto Involucro
Se esegui wrap() o fill() molte volte con le stesse impostazioni, è più efficiente creare un oggetto TextWrapper.
wrapper = textwrap.TextWrapper(width=30, max_lines=3, placeholder=' ~', initial_indent=' ')
s = "Python can be easy to pick up whether you're a first time programmer or you're experienced with other languages"
print(wrapper.wrap(s))
# [' Python can be easy to pick', "up whether you're a first time", "programmer or you're ~"]
print(wrapper.fill(s))
# Python can be easy to pick
# up whether you're a first time
# programmer or you're ~