Textwrap text style processing skills for python programming and development,
This article describes how to process textwrap text styles in python programming. We will share this with you for your reference. The details are as follows:
When reading python APIs, we found that python textwrap is powerful in processing string styles.
Here I made a demo:
Textwrap provides the following methods:
Wrap (text, width = 70, ** kwargs): This function Splits a string into a sequence.
From textwrap import * # Use The wrap () method def test_wrap () in textwrap: test_str = ''' \ The textwrap module provides two convenience funence, wrap () and fill (), as well as 1 TextWrapper, the class that does all the work, and two utility functions, dedent () and indent (). if 2 you're just wrapping or filling one or two text strings, the convenience funence shoshould be good 3 enough; otherwise, you shoshould use an instance of TextWrapper for efficiency. 4 ''' print (wrap (test_str, 20) def main (): test_wrap () if _ name _ = '_ main _': main ()
Output result:
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32Type "copyright", "credits" or "license()" for more information.>>> ================================ RESTART ================================>>> [' The textwrap', 'module provides two', 'convenience', 'functions, wrap()', 'and fill(), as well', 'as 1', 'TextWrapper, the', 'class that does all', 'the work, and two', 'utility functions,', 'dedent() and', 'indent(). If 2', 'you're just wrapping', 'or filling one or', 'two text strings,', 'the convenience', 'functions should be', 'good 3 enough;', 'otherwise, you', 'should use an', 'instance of', 'TextWrapper for', 'efficiency. 4']>>>
We will find that the wrap () function splits the string into a sequence in which the length of each element is the same.
Fill (text, width = 70, ** kwargs): This method splits strings Based on the specified length and displays them row by row.
From textwrap import * # fill () method def test_wrap (): test_str = ''' \ The textwrap module provides two convenience functions, wrap () and fill (), as well as 1 TextWrapper, the class that does all the work, and two utility functions, dedent () and indent (). if 2 you're just wrapping or filling one or two text strings, the convenience funence shoshould be good 3 enough; otherwise, you shoshould use an instance of TextWrapper for efficiency. 4 ''' print (fill (test_str, 40) def main (): test_wrap () if _ name _ = '_ main _': main ()
Running effect:
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32Type "copyright", "credits" or "license()" for more information.>>> ================================ RESTART ================================>>> The textwrap module provides twoconvenience functions, wrap() andfill(), as well as 1 TextWrapper,the class that does all the work, andtwo utility functions, dedent() andindent(). If 2 you're just wrappingor filling one or two text strings, theconvenience functions should be good 3enough; otherwise, you should use aninstance of TextWrapper for efficiency.>>>
Dedent () method-> text display without indentation, corresponding indent () method-> indent display
From textwrap import * # dedent () method def test_wrap (): test_str = ''' \ The textwrap module provides two convenience functions, wrap () and fill (), as well as 1 TextWrapper, the class that does all the work, and two utility functions, dedent () and indent (). if 2 you're just wrapping or filling one or two text strings, the convenience funence shoshould be good 3 enough; otherwise, you shoshould use an instance of TextWrapper for efficiency. 4 ''' print (repr (dedent (test_str) def main (): test_wrap () if _ name _ = '_ main __': main ()
Running effect:
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32Type "copyright", "credits" or "license()" for more information.>>> ================================ RESTART ================================>>> 'The textwrap module provides two convenience\n functions, wrap() and fill(), as well as 1\nTextWrapper, the class that does all the work,\n and two utility functions, dedent() and indent(). If 2\nyou're just wrapping or filling one or two text strings,\n the convenience functions should be good 3\nenough; otherwise, you should use an instance\n of TextWrapper for efficiency. 4\n'>>>
I hope this article will help you with Python programming.