textwrap — 文字换行和填充

源代码: Lib/textwrap.py


The textwrap module provides some convenience functions, as well as TextWrapper , the class that does all the work. If you’re just wrapping or filling one or two text strings, the convenience functions should be good enough; otherwise, you should use an instance of TextWrapper for efficiency.

textwrap. wrap ( text , width = 70 , * , initial_indent = '' , subsequent_indent = '' , expand_tabs = True , replace_whitespace = True , fix_sentence_endings = False , break_long_words = True , drop_whitespace = True , break_on_hyphens = True , tabsize = 8 , max_lines = None , placeholder = ' [...]' )

Wraps the single paragraph in text (a string) so every line is at most width characters long. Returns a list of output lines, without final newlines.

Optional keyword arguments correspond to the instance attributes of TextWrapper , documented below.

TextWrapper.wrap() method for additional details on how wrap() behaves.

textwrap. fill ( text , width = 70 , * , initial_indent = '' , subsequent_indent = '' , expand_tabs = True , replace_whitespace = True , fix_sentence_endings = False , break_long_words = True , drop_whitespace = True , break_on_hyphens = True , tabsize = 8 , max_lines = None , placeholder = ' [...]' )

Wraps the single paragraph in text , and returns a single string containing the wrapped paragraph. fill() is shorthand for

"\n".join(wrap(text, ...))