Python字符串expandtabs()方法

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!

上一篇: Python字符串 下一篇: Python列表