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

yipeiwu_com7年前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构建深度神经网络(续)

这篇文章在前一篇文章:python构建深度神经网络(DNN)的基础上,添加了一下几个内容: 1) 正则化项 2) 调出中间损失函数的输出 3) 构建了交叉损失函数 4) 将训练好的网络进...

python简单实现基于SSL的IRC bot实例

本文实例讲述了python简单实现基于SSL的 IRC bot。分享给大家供大家参考。具体如下: #!/usr/bin/python # -*- coding: utf8 -*- i...

windows10下python3.5 pip3安装图文教程

windows10下python3.5 pip3安装图文教程

最近Google官方的开发者博客中宣布新的版本Tensorflow(0.12)将增加对Windows的支持,想试着windows10下学习tensorflow,之前已经安装anacond...

Windows下用py2exe将Python程序打包成exe程序的教程

py2exe在sourceforge 的下载只支持到2.7。 针对python3.0+的版本,需要自己编译。 1.下载源码 svn checkout svn://svn.code.sf....

django中的图片验证码功能

django中的图片验证码功能

python的验证码库(captcha) 将验证码做成这样: 是不是和各大网页的图片源地址是一样,话不多说,让我们看代码: 我是用django和python中的captcha库做成 的...