當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python textwrap.dedent用法及代碼示例

用法:

textwrap.dedent(text)

text 的每一行中刪除任何常見的前導空格。

這可用於使triple-quoted 字符串與顯示的左邊對齊,同時仍以縮進形式在源代碼中顯示它們。

請注意,製表符和空格都被視為空格,但它們並不相等:"  hello""\thello" 行被認為沒有共同的前導空格。

僅包含空格的行在輸入中被忽略,並在輸出中標準化為單個換行符。

例如:

def test():
    # end first line with \ to avoid the empty line!
    s = '''\
    hello
      world
    '''
    print(repr(s))          # prints '    hello\n      world\n    '
    print(repr(dedent(s)))  # prints 'hello\n  world\n'

相關用法


注:本文由純淨天空篩選整理自python.org大神的英文原創作品 textwrap.dedent。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。