Skip to main content

Python 字符串的startswith和endswith函数

Published 1 min readMarkdown
Python 字符串的startswith和endswith函数

字符串的startswith和endswith函数

功能

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

用法

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

小发现

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

代码

# 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)

No comments yet. Be the first to comment.

More from this blog