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
相关推荐
yejqvow121 分钟前
Redis如何处理集群网络分区_理解少数派网络孤岛由于无法获得选票而停止写入的保护机制
jvm·数据库·python
21439653 分钟前
CSS Grid布局如何解决图片溢出网格单元_设置object-fit与网格尺寸
jvm·数据库·python
qq_654366983 分钟前
C#怎么实现动态加载DLL C#如何在运行时动态加载和卸载程序集调用其中的方法【进阶】
jvm·数据库·python
2301_764150564 分钟前
WooCommerce 用户登录状态控制元素显隐的 CSS 实现方案
jvm·数据库·python
m0_743623924 分钟前
CSS如何解决响应式布局中文字溢出_通过text-overflow-ellipsis处理
jvm·数据库·python
2401_835956814 分钟前
Go语言中 & 与 -:指针取址与解引用的完整解析
jvm·数据库·python
2401_837163896 分钟前
如何为容器内多个列表实现统一滚动条
jvm·数据库·python
m0_674294647 分钟前
C#怎么清空Dictionary字典_C#如何管理内存集合【基础】
jvm·数据库·python
2402_854808378 分钟前
MongoDB GridFS分片时选择什么键比较好
jvm·数据库·python
2301_796588509 分钟前
mysql如何统计不同状态的数量_使用group by配合count函数
jvm·数据库·python