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
相关推荐
2402_85480837几秒前
c++ RAII机制详解 c++如何利用RAII管理资源
jvm·数据库·python
talen_hx296几秒前
飞书机器人发文本消息
java·前端·飞书
吕源林1 分钟前
CSS如何使用Less的Merge功能合并多个属性值_通过逗号或空格组织css参数
jvm·数据库·python
qq_330037992 分钟前
Go语言如何写负载均衡器_Go语言负载均衡器实战教程【完整】
jvm·数据库·python
2601_949816582 分钟前
Spring Boot--@PathVariable、@RequestParam、@RequestBody
java·spring boot·后端
2501_914245933 分钟前
如何验证SQL删除操作的影响行数_通过ROW_COUNT获取反馈
jvm·数据库·python
2301_816660213 分钟前
如何处理DG Broker的ORA-16664错误_主备库网络通信与TNS配置排查
jvm·数据库·python
2401_835956815 分钟前
mysql如何配置用户只读权限_授予SELECT权限与限制操作
jvm·数据库·python
freewlt5 分钟前
前端工程化性能监控体系建设:从0到1实战指南
前端
weixin_408717776 分钟前
HTML怎么标注成就连续打卡中断_HTML“断连,重新开始”提示【方法】
jvm·数据库·python