2017-10-06 86 views
0
sample_text = ''' 
     The textwrap module can be used to format text for output in 
     situations where pretty-printing is desired. It offers 
     programmatic functionality similar to the paragraph wrapping 
     or filling features found in many text editors. 
    ''' 
dedented_text = textwrap.dedent(sample_text) 
wrapped = textwrap.fill(dedented_text, width=50) 
final = textwrap.indent(wrapped, '> ') 

print('Quoted block:\n') 
print(final) 

輸出爲:的額外空間,當使用textwrap.indent()

> The textwrap module can be used to format text 
> for output in situations where pretty-printing is 
> desired. It offers programmatic functionality 
> similar to the paragraph wrapping or filling 
> features found in many text editors. 

只是想了解爲什麼會出現在年初的第一行額外的空間?

回答

2

看看repr(sample_text)

'\n  The textwrap module can be used to format text for output in\n  situations where pretty-printing is desired. It offers\n  programmatic functionality similar to the paragraph wrapping\n  or filling features found in many text editors.\n ' 

通知的\n開頭?

爲了達到您想要的輸出效果,您必須將其轉義。把\放在字符串的開頭:

sample_text = '''\ 
    The textwrap module can be used to format text for output in 
    situations where pretty-printing is desired. It offers 
    programmatic functionality similar to the paragraph wrapping 
    or filling features found in many text editors. 
''' 
+0

知道了!謝謝!! –