To format a string in Python by wrapping (line breaking) and truncating (abbreviating) it at an arbitrary number of characters, use the textwrap module of the standard library.
The following information is provided here.
- Wrapping a string (line feed):
wrap()
,fill()
- Truncate strings (omitted):
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.
- Related Articles:Writing long strings of text on multiple lines in Python
Wrapping a string (line feed): wrap(), fill()
With the function wrap() of the textwrap module, you can get a list divided by word breaks to fit into an arbitrary 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']
Using the obtained list, you can get a string that is broken by a newline code by doing the following'\n'.join(list)
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 function fill() returns a newline string instead of a list. It is the same as executing the following code after wrap() as in the example above.'\n'.join(list)
This is more convenient when you don't need a list but want to output a fixed-width string to a terminal, etc.
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 number of lines after it will be 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, the following string will be output at the end by default.' [...]'
It can be replaced by any string with the argument placeholder.
print(textwrap.fill(s, 40, max_lines=2, placeholder=' ~'))
# Python can be easy to pick up whether
# you're a first time programmer or ~
You can also specify a string to be added to the beginning of the first line with the argument initial_indent. This can be 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 ~
Be careful with full-size and half-size characters.
In textwrap, the number of characters is controlled by the number of characters, not by the character width, and both single-byte and double-byte characters are considered as one character.
s = '文字文字文字文字文字文字12345,67890, 文字文字文字abcde'
print(textwrap.fill(s, 12))
# 文字文字文字文字文字文字
# 12345,67890,
# 文字文字文字abcde
If you want to wrap a text with mixed kanji characters with a fixed width, please refer to the following.
Truncate strings (omitted): shorten()
If you want to truncate and omit strings, use the function shorten() in the textwrap module.
Abbreviated in word units to fit an arbitrary number of characters. The number of characters, including the string indicating the omission, is arbitrary. The string indicating the omission can be set with the argument placeholder, which defaults to the following.' [...]'
s = 'Python is powerful'
print(textwrap.shorten(s, 12))
# Python [...]
print(textwrap.shorten(s, 12, placeholder=' ~'))
# Python is ~
However, Japanese strings, for example, cannot be abbreviated well because they cannot be divided into words.
s = 'Pythonについて。Pythonは汎用のプログラミング言語である。'
print(textwrap.shorten(s, 20))
# [...]
If you want to abbreviate by considering only the number of characters instead of word units, it can be easily achieved as follows.
s_short = s[:12] + '...'
print(s_short)
# Pythonについて。P...
TextWrapper object
If you are going to wrap() or fill() many times with a fixed configuration, it is 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 ~
The same settings can be reused.