Python expandtabs()与endswith()方法

Python expandtabs()方法

描述

Python expandtabs() 方法把字符串中的 tab 符号('\t')转为空格,默认的空格数 tabsize 是 8。

语法

expandtabs()方法语法:

复制代码
string.expandtabs(tabsize=8)

参数

tabsize -- 指定转换字符串中的 tab 符号('\t')转为空格的字符数。

返回值

该方法返回字符串中的 tab 符号('\t')转为空格后生成的新字符串。

实例

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

复制代码
#!/usr/bin/python
 
string = "this is\tstring example....wow!!!";
 
print "Original string: " + string
print "Defualt exapanded tab: " +  string.expandtabs()
print "Double exapanded tab: " +  string.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 endswith()方法

描述

Python endswith() 方法用于判断字符串是否以指定后缀结尾,如果以指定后缀结尾返回True,否则返回False。可选参数"start"与"end"为检索字符串的开始与结束位置。

语法

endswith()方法语法:

复制代码
string.endswith(suffix[, start[, end]])
# 自Python2.5版本起,还支持接收一个 tuple 为参数
string.endswith(tuple) # 满足tuple任何一个都返回True

参数

suffix -- 该参数可以是一个字符串或者是一个元素This could be a string or could also be a tuple of suffixes to look for.

start -- 字符串中的开始位置。

end -- 字符中结束位置。

返回值

如果字符串含有指定的后缀返回True,否则返回False。

实例

以下实例展示了endswith()方法的实例:

复制代码
#!/usr/bin/python
 
string = "this is string example....wow!!!";
 
suffix = "wow!!!";
print string.endswith(suffix);
print string.endswith(suffix,20);
 
suffix = "is";
print string.endswith(suffix, 2, 4);
print string.endswith(suffix, 2, 6);
 
print '-----------------------------'
 
string1 = 'abc'
string2 = 'xyz'
string3 = 'twz'
for string in [string1,string2,string3]:
    print string.endswith(('a','x'))

以上实例输出结果如下:

复制代码
True
True
True
False
-----------------------------
True
True
False
相关推荐
N_NAN_N3 分钟前
类图+案例+代码详解:软件设计模式----单例模式
java·单例模式·设计模式
小磊哥er5 分钟前
【前端工程化】你知道前端编码规范包含哪些内容吗
前端
weixin_3993806911 分钟前
k8s一键部署tongweb企业版7049m6(by why+lqw)
java·linux·运维·服务器·云原生·容器·kubernetes
lang2015092818 分钟前
Reactor ConnectableFlux支持多订阅者
java·网络
菌菇汤20 分钟前
uni-app实现单选,多选也能搜索,勾选,选择,回显
前端·javascript·vue.js·微信小程序·uni-app·app
R-sz23 分钟前
java流式计算 获取全量树形数据,非懒加载树,递归找儿
java·开发语言·windows
Ramos丶28 分钟前
【ABAP】 从无到有 新建一个Webdynpro程序
java·前端·javascript
sniper_fandc39 分钟前
SpringMVC详解
java·springmvc
qq_411671981 小时前
vue3 的模板引用ref和$parent
前端·javascript·vue.js
Gyoku Mint1 小时前
深度学习×第4卷:Pytorch实战——她第一次用张量去拟合你的轨迹
人工智能·pytorch·python·深度学习·神经网络·算法·聚类