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
相关推荐
Justin3go3 小时前
HUNT0 上线了——尽早发布,尽早发现
前端·后端·程序员
怕浪猫4 小时前
第一章 JSX 增强特性与函数组件入门
前端·javascript·react.js
铅笔侠_小龙虾4 小时前
Emmet 常用用法指南
前端·vue
没有bug.的程序员4 小时前
服务安全:内部服务如何防止“裸奔”?
java·网络安全·云原生安全·服务安全·零信任架构·微服务安全·内部鉴权
钦拆大仁4 小时前
跨站脚本攻击XSS
前端·xss
一线大码5 小时前
SpringBoot 3 和 4 的版本新特性和升级要点
java·spring boot·后端
秃了也弱了。5 小时前
python实现定时任务:schedule库、APScheduler库
开发语言·python
Dfreedom.5 小时前
从 model(x) 到__call__:解密深度学习框架的设计基石
人工智能·pytorch·python·深度学习·call
weixin_440730505 小时前
java数组整理笔记
java·开发语言·笔记
weixin_425023005 小时前
Spring Boot 配置文件优先级详解
spring boot·后端·python