字符串的startswith和endswith函数

功能

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

用法

  • string.startswith(item) -> item : 你想查询匹配的元素,返回一个布尔值
  • string.endswith(item) -> item: 你想查询匹配的元素,返回一个布尔值

小发现

item赋值为''时,始终返回为True

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 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)

result = info.endswith('this is a string example!!')
print('full?:', result)