1. 程式人生 > 其它 >Python字串:str.startswith(prefix[, start[, end]])和str.endswith(suffix[, start[, end]])

Python字串:str.startswith(prefix[, start[, end]])和str.endswith(suffix[, start[, end]])

技術標籤:# 4.2 Pythonpython字串startswithendswith

str.startswith(prefix[, start[, end]])

如果字串以指定的 prefix 開始則返回 True,否則返回 False。 prefix 也可以為由多個供查詢的字首構成的元組。 如果有可選項 start,將從所指定位置開始檢查。 如果有可選項 end,將在所指定位置停止比較。

str.endswith(suffix[, start[, end]])

如果字串以指定的 suffix 結束返回 True,否則返回 False。 suffix 也可以為由多個供查詢的字尾構成的元組。 如果有可選項 start,將從所指定位置開始檢查。 如果有可選項 end,將在所指定位置停止比較。

# 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('this is a string example!!')
print('result:', result)

在這裡插入圖片描述