python关于字符串的内置函数_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)) # 结果:True

print(str.endswith(suffix, 20)) # 结果:True

suffix = "is";

print(str.endswith(suffix, 2, 4)) # 结果:True

print(str.endswith(suffix, 2, 6)) # 结果:False

# startswith()函数用于检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False。如果参数 beg 和 end 指定值,则在指定范围内检查。

str = "this is string example....wow!!!";

print(str.startswith( 'this' )) # 结果:True

print(str.startswith( 'is', 2, 4 )) # 结果:True

print(str.startswith( 'this', 2, 4 )) # 结果:False

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值