طريقة Python String rfind ()
مثال
أين يوجد آخر تكرار للسلسلة "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"))