Python 字符串内置函数(二)

# 2.格式化相关# ljust(width)  函数   获取固定长度,左对齐,右边不够用空格补齐# rjust(width)  函数   获取固定长度,右对齐,左边不够用空格补齐# center(width) 函数   获取固定长度,中间对齐,两边不够用空格补齐# zfill(width)  函数   获取固定长度,右对齐,左边不足用0补齐# format()      函数   字符串格式化的功能# endswith()    函数   用于判断字符串是否以指定后缀结尾,若是以指定后缀结尾返回True,不然返回False。可选参数"start"与"end"为检索字符串的开始与结束位置。# startswith()  函数   用于检查字符串是不是以指定子字符串开头,若是是则返回 True,不然返回 False。若是参数 beg 和 end 指定值,则在指定范围内检查。a='1 2'print(a.ljust(10))print(a.rjust(10))print(a.center(10))print(a.zfill(10))'''执行结果:1 2              1 2   1 2    00000001 2'''# format()字符串格式化的功能print("{1} {0} {1}".format("hello", "world"))  # 设置指定位置# 结果:'world hello world'print("网站名:{name}, 地址 {url}".format(name="菜鸟教程", url="www.runoob.com"))# 结果:网站名:菜鸟教程, 地址 www.runoob.com# endswith()函数用于判断字符串是否以指定后缀结尾,若是以指定后缀结尾返回True,不然返回False。可选参数"start"与"end"为检索字符串的开始与结束位置。str = "this is string example....wow!!!";suffix = "wow!!!";print(str.endswith(suffix)) # 结果:Trueprint(str.endswith(suffix, 20)) # 结果:Truesuffix = "is";print(str.endswith(suffix, 2, 4)) # 结果:Trueprint(str.endswith(suffix, 2, 6)) # 结果:False# startswith()函数用于检查字符串是不是以指定子字符串开头,若是是则返回 True,不然返回 False。若是参数 beg 和 end 指定值,则在指定范围内检查。str = "this is string example....wow!!!";print(str.startswith( 'this' )) # 结果:Trueprint(str.startswith( 'is', 2, 4 )) # 结果:Trueprint(str.startswith( 'this', 2, 4 )) # 结果:False
相关文章
相关标签/搜索