Python字符串startswith和endswith函数

目录

  • startswith和endswith的功能
  • startswitch和endswitch的用法

startswith和endswith功能

  • startswith 判断字符串开始位是否是某成员(元素)
  • endswith判断字符串结尾是否是某成员(元素)

startswith和endswith用法

  • 用法:
    • string.startswith(item)
    • string.endswith(item)
    • item:你想查询匹配的元素,返回一个布尔值
In [20]: 'my name is insane'.startswith('my')
out [20]: True
In [21]: 'my name is insane'.endswith('my')
out [21]: False

实战

# coding:utf-8

info = 'this is a string example!'

result = info.startswith('this')
print(result)

result = info.startswith('this is a string example!')
print(result)

print(bool(info == 'this is a string example!'))

result = info.endswith('!')
print(result)

result = info.endswith('a')
print('result:', result)

result = info.endswith('this is a string example!')
print(result)
True
True
True
True
result: False
True

Process finished with exit code 0
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值