Python 字符串 startswith() 方法
實例
檢查字符串是否以 "Hello" 開頭:
txt = "Hello, welcome to my world." x = txt.startswith("Hello") print(x)
定義和用法
如果字符串以指定的值開頭,則 startswith() 方法返回 True,否則返回 False。
語法
string.startswith(value, start, end)
參數值
參數 | 描述 |
---|---|
value | 必需。檢查字符串是否以其開頭的值。 |
start | 可選。整數,規定從哪個位置開始搜索。 |
end | 可選。整數,規定結束搜索的位置。 |
更多實例
實例
檢查位置 7 到 20 是否以字符 "wel" 開頭:
txt = "Hello, welcome to my world." x = txt.startswith("wel", 7, 20) print(x)