How to wrap and indent for long lines when using print ()?

I use Python to collect data from a web service. The data itself is a list that will be presented to users. I managed it that it printed as:

( 1) Example of strings ( 2) Example 4 ( 3) Another Example ( 4) Another Example 2 

I use rjust (2) for numbers. However, if the lines are very long, the print will be similar. The gap width is the terminal length (rxvt, gnome terminal):

 ( 1) Example of a very very very very long string and that doesn't look fine ( 2) Example of indented long line that is very very long, example line ( 3) Another Example ( 4) Another Example of a very very long string and whatever here is 

But I want to print as:

 ( 1) Example of a very very very very long string and that doesn't look fine ( 2) Example of indented long line that is very very long, example line ( 3) Another Example ( 4) Another Example of a very very long string and whatever here is 

Any ideas how to print the lines as above without breaking the lines manually?

+4
source share
1 answer

Use the textwrap module: http://docs.python.org/library/textwrap.html

  textwrap.fill(text, initial_indent='', subsequent_indent=' ') 
+8
source

All Articles