溫馨提示×

python中find()的使用方法是什么

小億
84
2024-04-19 17:04:03
欄目: 編程語言

在Python中,find()方法用于查找字符串中是否包含指定的子字符串。該方法會返回子字符串在原始字符串中第一次出現的索引位置,如果沒有找到則返回-1。

string = "Hello, World!"
index = string.find("World")
print(index)  # 輸出:7

index = string.find("Python")
print(index)  # 輸出:-1

find()方法還可以指定起始和結束索引,以限制搜索范圍:

string = "Hello, World!"
index = string.find("o", 5)
print(index)  # 輸出:7

index = string.find("o", 5, 8)
print(index)  # 輸出:-1

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女