Python3 expandtabs()方法

Python3 字符串 Python3 字符串


描述

expandtabs() 方法把字符串中的 tab 符號('\t')轉(zhuǎn)為空格,tab 符號('\t')默認的空格數(shù)是 8。

語法

expandtabs()方法語法:

str.expandtabs(tabsize=8)

參數(shù)

  • tabsize -- 指定轉(zhuǎn)換字符串中的 tab 符號('\t')轉(zhuǎn)為空格的字符數(shù)。

返回值

該方法返回字符串中的 tab 符號('\t')轉(zhuǎn)為空格后生成的新字符串。

實例

以下實例展示了expandtabs()方法的實例:

#!/usr/bin/python3

str = "this is\tstring example....wow!!!"

print ("原始字符串: " + str)
print ("替換 \\t 符號: " +  str.expandtabs())
print ("使用16個空格替換 \\t 符號: " +  str.expandtabs(16))

以上實例輸出結(jié)果如下:

原始字符串: this is     string example....wow!!!
替換 \t 符號: this is string example....wow!!!
使用16個空格替換 \t 符號: this is         string example....wow!!!

Python3 字符串 Python3 字符串