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)
自己動手試一試 »