Python String rfind() Method
ตัวอย่าง
ตำแหน่ง สุดท้ายของสตริง "casa" อยู่ที่ไหนในข้อความ :
txt = "Mi
casa, su casa."
x = txt.rfind("casa")
print(x)
ความหมายและการใช้งาน
วิธีrfind()
ค้นหาการเกิดขึ้นล่าสุดของค่าที่ระบุ
วิธี การrfind()
คืนค่า -1 หากไม่พบค่า
วิธี การrfind()
เกือบจะเหมือนกับ
rindex()
วิธีการ ดูตัวอย่างด้านล่าง
ไวยากรณ์
string.rfind(value, start, end)
ค่าพารามิเตอร์
Parameter | Description |
---|---|
value | Required. The value to search for |
start | Optional. Where to start the search. Default is 0 |
end | Optional. Where to end the search. Default is to the end of the string |
ตัวอย่างเพิ่มเติม
ตัวอย่าง
ตำแหน่งสุดท้ายของตัวอักษร "e" อยู่ที่ไหนในข้อความ:
txt = "Hello, welcome to my world."
x = txt.rfind("e")
print(x)
ตัวอย่าง
ตำแหน่งใดในข้อความที่เกิดครั้งสุดท้ายของตัวอักษร "e" เมื่อคุณค้นหาระหว่างตำแหน่ง 5 ถึง 10 เท่านั้น:
txt = "Hello, welcome to my world."
x = txt.rfind("e",
5, 10)
print(x)
ตัวอย่าง
หากไม่พบค่า เมธอด rfind() จะคืนค่า -1 แต่เมธอด rindex() จะทำให้เกิดข้อยกเว้น:
txt = "Hello, welcome to my world."
print(txt.rfind("q"))
print(txt.rindex("q"))