在python中以正确的方式拆分长线打印到屏幕上
这可能是一个愚蠢的问题,但我想知道其他人是如何处理这个问题的,或者是否有一个标准/推荐的方法 下面是在python中将长文本行打印到屏幕上时拆分该行的两种方法。应该使用哪一种 选项1在python中以正确的方式拆分长线打印到屏幕上,python,pep8,Python,Pep8,这可能是一个愚蠢的问题,但我想知道其他人是如何处理这个问题的,或者是否有一个标准/推荐的方法 下面是在python中将长文本行打印到屏幕上时拆分该行的两种方法。应该使用哪一种 选项1 if some_condition: # Senseless indenting. if another condition: # Senseless indenting. print 'This is a very long line of text that goes beyond th
if some_condition: # Senseless indenting.
if another condition: # Senseless indenting.
print 'This is a very long line of text that goes beyond the 80\n\
character limit.'
选项2
if some_condition: # Senseless indenting.
if another condition: # Senseless indenting.
print 'This is a very long line of text that goes beyond the 80'
print 'character limit.'
我个人觉得选项1很难看,但选项2似乎不符合通过第二次
打印
调用使事情简单化的python方法。一种方法是使用括号:
print ('This is a very long line of text that goes beyond the 80\n'
'character limit.')
当然,有几种方法可以做到这一点。另一种方式(如评论中所建议的)是三重引用:
print '''This is a very long line of text that goes beyond the 80
character limit.'''
就我个人而言,我不太喜欢这一点,因为它似乎打破了缩进,但这只是我自己。一种方法是使用括号:
print ('This is a very long line of text that goes beyond the 80\n'
'character limit.')
当然,有几种方法可以做到这一点。另一种方式(如评论中所建议的)是三重引用:
print '''This is a very long line of text that goes beyond the 80
character limit.'''
就我个人而言,我不太喜欢这一点,因为它似乎打破了缩进,但这只是我自己。一种方法是使用括号:
print ('This is a very long line of text that goes beyond the 80\n'
'character limit.')
当然,有几种方法可以做到这一点。另一种方式(如评论中所建议的)是三重引用:
print '''This is a very long line of text that goes beyond the 80
character limit.'''
就我个人而言,我不太喜欢这一点,因为它似乎打破了缩进,但这只是我自己。一种方法是使用括号:
print ('This is a very long line of text that goes beyond the 80\n'
'character limit.')
当然,有几种方法可以做到这一点。另一种方式(如评论中所建议的)是三重引用:
print '''This is a very long line of text that goes beyond the 80
character limit.'''
就个人而言,我不太喜欢这一点,因为它看起来像是破坏缩进,但这只是我自己。如果您有一个长字符串,并希望在适当的点插入换行符,则
textwrap
模块提供了这样做的功能。例:
import textwrap
def format_long_string(long_string):
wrapper = textwrap.TextWrapper()
wrapper.width = 80
return wrapper.fill(long_string)
long_string = ('This is a really long string that is raw and unformatted '
'that may need to be broken up into little bits')
print format_long_string(long_string)
这将导致打印以下内容:
This is a really long string that is raw and unformatted that may need to be
broken up into little bits
如果您有一个长字符串,并且希望在适当的点插入换行符,则
textwrap
模块提供了相应的功能。例:
import textwrap
def format_long_string(long_string):
wrapper = textwrap.TextWrapper()
wrapper.width = 80
return wrapper.fill(long_string)
long_string = ('This is a really long string that is raw and unformatted '
'that may need to be broken up into little bits')
print format_long_string(long_string)
这将导致打印以下内容:
This is a really long string that is raw and unformatted that may need to be
broken up into little bits
如果您有一个长字符串,并且希望在适当的点插入换行符,则
textwrap
模块提供了相应的功能。例:
import textwrap
def format_long_string(long_string):
wrapper = textwrap.TextWrapper()
wrapper.width = 80
return wrapper.fill(long_string)
long_string = ('This is a really long string that is raw and unformatted '
'that may need to be broken up into little bits')
print format_long_string(long_string)
这将导致打印以下内容:
This is a really long string that is raw and unformatted that may need to be
broken up into little bits
如果您有一个长字符串,并且希望在适当的点插入换行符,则
textwrap
模块提供了相应的功能。例:
import textwrap
def format_long_string(long_string):
wrapper = textwrap.TextWrapper()
wrapper.width = 80
return wrapper.fill(long_string)
long_string = ('This is a really long string that is raw and unformatted '
'that may need to be broken up into little bits')
print format_long_string(long_string)
这将导致打印以下内容:
This is a really long string that is raw and unformatted that may need to be
broken up into little bits
就个人而言,我会使用三重引用。就个人而言,我会使用三重引用。就个人而言,我会使用三重引用。就个人而言,我会使用三重引用。