note.nkmk.me

Wrap and truncate a string with textwrap in Python

Posted: 2021-10-03 / Tags: Python, String

In Python, to wrap or truncate a string at a given width (= number of characters), use the textwrap module of the standard library.

This article describes the following contents.

  • Wrap a string: wrap(), fill()
  • Truncate a string: shorten()
  • TextWrapper object

If you want to write long strings on multiple lines in the code instead of in the output, see the following article.

The pprint module is useful for formatting and outputting lists and dictionaries. See the following article.

Sponsored Link

Wrap a string: wrap(), fill()

With the wrap() function of the textwrap module, you can get a list divided by word breaks to fit a given number of characters.

Specify the number of characters for the second argument width. The default is width=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']

Use '\n'.join(list) to get the string with the line feed code \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

The fill() function returns a string with line breaks, not a list. It is the same as '\n'.join(list) after wrap() as in the example above.

This is convenient when you don't need a list and want to output a fixed-width string.

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

If the argument max_line is specified, the following lines are omitted.

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 [...]

If omitted, by default, ' [...]' is output at the end. It can be replaced by any string with the placeholder argument.

print(textwrap.fill(s, 40, max_lines=2, placeholder=' ~'))
# Python can be easy to pick up whether
# you're a first time programmer or ~

The argument initial_indent can be used to specify a string to be added at the beginning of the first line. It is used when you want to indent the beginning of a paragraph.

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 ~

For other detailed settings, please refer to the official documentation.

Truncate a string: shorten()

If you want to shorten a string by truncating it, use the function shorten() of the textwrap module.

It can be truncated word by word to fit into a given number of characters with the placeholder. The placeholder is ' [...]' by default. and can be set with the placeholder argument.

s = 'Python is powerful'

print(textwrap.shorten(s, 12))
# Python [...]

print(textwrap.shorten(s, 12, placeholder=' ~'))
# Python is ~
Sponsored Link

TextWrapper object

If you do wrap() or fill() many times with the same settings, it is more efficient to create a TextWrapper object.

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 ~
Sponsored Link
Share

Related Categories

Related Articles