详解Python中expandtabs()方法的使用

yipeiwu_com6年前Python基础

 expandtabs()方法返回制表符,即该字符串的一个副本。 '\t'已经使用的空间,可选择使用给定的tabsize(默认8)扩展。
语法

以下是expandtabs()方法的语法:

str.expandtabs(tabsize=8)

参数

  •     tabsize -- 此选项指定要替换为制表符“\t' 的字符数.

返回值

此方法返回在制表符,即通过空格进行了扩展字符串,'\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使用arp欺骗伪造网关的方法

本文实例讲述了python使用arp欺骗伪造网关的方法。分享给大家供大家参考。具体实现方法如下: #coding:utf-8 ''' arp欺骗局域网pc,将伪造的网关mac以网关的...

详解python破解zip文件密码的方法

详解python破解zip文件密码的方法

1、单线程破解纯数字密码 注意: 不包括数字0开头的密码 import zipfile,time,sys start_time = time.time() def extract()...

Python简单读写Xls格式文档的方法示例

Python简单读写Xls格式文档的方法示例

本文实例讲述了Python简单读写Xls格式文档的方法。分享给大家供大家参考,具体如下: 1. 模块安装 使用pip install命令安装, 即: pip install xlrd...

用python实现面向对像的ASP程序实例

本文实例讲述了用python实现面向对像的ASP程序的方法。分享给大家供大家参考。具体实现方法如下: 平时我们写ASP时,一般都用vbscript或javascript. javascr...

Python类的用法实例浅析

本文实例讲述了Python类的用法。分享给大家供大家参考。具体如下: 先看一段代码: #!/usr/bin/env python class Test: def __init__...