Latest web development tutorials

Python expandtabs()方法

Python 字符串 Python字符串


描述

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

語法

expandtabs()方法語法:

str.expandtabs(tabsize=8)

參數

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

返回值

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

實例

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

#!/usr/bin/python

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


print "Original string: " + str;
print "Defualt exapanded tab: " +  str.expandtabs();
print "Double exapanded tab: " +  str.expandtabs(16);

以上實例輸出結果如下:

Original string: this is        string example....wow!!!
Defualt exapanded tab: this is string example....wow!!!
Double exapanded tab: this is         string example....wow!!!

Python 字符串 Python字符串