Python PEP8 printing wrapped strings without indent

前端 未结 3 1375
一个人的身影
一个人的身影 2021-02-20 06:31

There is probably an easy answer for this, just not sure how to tease it out of my searches.

I adhere to PEP8 in my python code, and I\'m currently using OptionParser fo

相关标签:
3条回答
  • 2021-02-20 06:48

    Use automatic string concatenation + implicit line continuation:

    long_string = ("Line 1 "
                   "Line 2 "
                   "Line 3 ")
    
    
    >>> long_string
    'Line 1 Line 2 Line 3 '
    
    0 讨论(0)
  • 2021-02-20 06:55

    try this:

    if __name__=='__main__':
        usage = '%prog [options]\nWithout any options, will display 10 random ' \
        'users of each type.'
        parser = OptionParser(usage)
    
    0 讨论(0)
  • 2021-02-20 07:02

    This works:

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

    Although I'd lay it out like this:

    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's a \n in the string, as well as when I need to word wrap the source code.)

    0 讨论(0)
提交回复
热议问题