Python PEP8 print wrapped lines without indentation

There is probably a simple answer for this, just not sure how to mock my searches.

I adhere to PEP8 in my Python code and now I am using OptionParser for the script that I am writing. So that the lines do not exceed 80, I use backslash when necessary.

For example:

if __name__=='__main__': usage = '%prog [options]\nWithout any options, will display 10 random \ users of each type.' parser = OptionParser(usage) 

This indent after a backslash results in:

 ~$ ./er_usersearch -h Usage: er_usersearch [options] Without any options, will display 10 random users of each type. 

This gap after the "random" errors of me. I could do:

  if __name__=='__main__': usage = '%prog [options]\nWithout any options, will display 10 random \ users of each type.' parser = OptionParser(usage) 

But it makes me so terrible. This seems silly:

  if __name__=='__main__': usage = ''.join(['%prog [options]\nWithout any options, will display', ' 10 random users of each type.']) parser = OptionParser(usage) 

Should there be a better way?

+11
python wrapping pep8
Aug 19 '09 at 20:10
source share
3 answers

Use automatic string concatenation + implicit line continuation :

 long_string = ("Line 1 " "Line 2 " "Line 3 ") >>> long_string 'Line 1 Line 2 Line 3 ' 
+28
Aug 19 '09 at 20:13
source share

It works:

 if __name__=='__main__': usage = ('%prog [options]\nWithout any options, will display 10 random ' 'users of each type.') parser = OptionParser(usage) 

Although I would describe it as follows:

 if __name__=='__main__': usage = ('%prog [options]\n' 'Without any options, will display 10 random users ' 'of each type.') parser = OptionParser(usage) 

(So, I start a new line when there is \n in the line, and also when I need to add the end of the source code.)

+3
Aug 19 '09 at 20:13
source share

try the following:

 if __name__=='__main__': usage = '%prog [options]\nWithout any options, will display 10 random ' \ 'users of each type.' parser = OptionParser(usage) 
+1
Aug 19 '09 at 20:13
source share



All Articles