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
相关推荐
quant_19863 分钟前
全面解析美股行情API
经验分享·后端·python·websocket·程序人生·区块链
Danceful_YJ3 分钟前
32.Bahdanau 注意力
pytorch·python·深度学习
随便叫个啥呢4 分钟前
java使用poi-tl模版+vform自定义表单生成word,使用LibreOffice导出为pdf,批量下载为压缩文件
java·pdf·word·zip
CodeCraft Studio4 分钟前
国产化Word处理控件Spire.Doc教程:使用Java将RTF文件转换为PDF的全面教程
java·pdf·word·spire.doc·rtf转pdf·文件格式转换·文档开发sdk
转角羊儿5 分钟前
layui框架中,表单元素不显示问题
前端·javascript·layui
随便叫个啥呢7 分钟前
java使用poi-tl模版+vform自定义表单生成word
java·word·poi-tl
muyouking1124 分钟前
WASM 3.0 两大领域实战:SvelteKit前端新范式(完整版)
前端·wasm
开心星人25 分钟前
Leetcode hot100 Java刷题(二)
java·算法·leetcode
喵手25 分钟前
Java与Microservices架构的结合:构建可扩展、高可用的系统!
java·架构·华为云
曲鸟31 分钟前
用Python和MediaPipe实现实时手指识别
开发语言·python