طريقة Python String rindex ()
مثال
أين يوجد آخر تكرار للسلسلة "casa" في النص ؟:
txt = "Mi
casa, su casa."
x = txt.rindex("casa")
print(x)
التعريف والاستخدام
يعثر الأسلوب rindex()
على آخر تواجد للقيمة المحددة.
تثير rindex()
الطريقة استثناء إذا لم يتم العثور على القيمة.
الطريقة rindex()
هي تقريبا نفس
rfind()
الطريقة. انظر المثال أدناه.
بناء الجملة
string.rindex(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.rindex("e")
print(x)
مثال
أين يوجد آخر ظهور للحرف "e" في النص عندما تبحث فقط بين الموضعين 5 و 10 ؟:
txt = "Hello, welcome to my world."
x = txt.rindex("e",
5, 10)
print(x)
مثال
إذا لم يتم العثور على القيمة ، فإن طريقة rfind () ترجع -1 ، لكن طريقة rindex () ستثير استثناءً:
txt = "Hello, welcome to my world."
print(txt.rfind("q"))
print(txt.rindex("q"))