Python字串expandtabs()
方法返回一個字串的副本,其中tab
字元。使用空格擴展’\t
‘,可選地使用給定的跳位字元大小 - tabize
(默認值為8
)。
語法
以下是expandtabs()
方法的語法 -
str.expandtabs(tabsize = 8)
參數
- tabsize - 這指定了替換字元“
\t
”要替換的字元數。
返回值
- 此方法返回一個字串的副本,其中使用空格擴展了“
\t
”字元。
示例
以下示例顯示了expandtabs()
方法的用法。
#! /usr/bin/env python
#coding=utf-8
# save file: string_expandtabs.py
str = "Welcome\tto\tzaixian python toturitals!"
print ("Original string: " + str)
print ("Defualt exapanded tab: " + str.expandtabs())
print ("Double exapanded tab: " + str.expandtabs(16))
當運行上面的程式,它產生以下結果 -
Original string: Welcome to zaixian python toturitals!
Defualt exapanded tab: Welcome to zaixian python toturitals!
Double exapanded tab: Welcome to zaixian python toturitals!