पायथन स्ट्रिंग इंडेक्स () विधि
उदाहरण
पाठ में "स्वागत" शब्द कहाँ है ?:
txt = "Hello, welcome to my world."
x = txt.index("welcome")
print(x)
परिभाषा और उपयोग
index()
विधि निर्दिष्ट मान की पहली घटना का पता लगाती है ।
यदि index()
मान नहीं मिलता है तो विधि अपवाद उठाती है।
index()
विधि लगभग विधि के समान ही है
, find()
अंतर केवल इतना है कि find()
यदि मान नहीं मिलता है तो विधि -1 लौटाती है। (नीचे उदाहरण देखें)
वाक्य - विन्यास
string.index(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 |
और ज्यादा उदाहरण
उदाहरण
पाठ में "ई" अक्षर की पहली घटना कहाँ है ?:
txt = "Hello, welcome to my world."
x = txt.index("e")
print(x)
उदाहरण
जब आप केवल स्थिति 5 और 10 के बीच खोज करते हैं तो पाठ में "ई" अक्षर की पहली आवृत्ति कहां होती है?:
txt = "Hello, welcome to my world."
x = txt.index("e",
5, 10)
print(x)
उदाहरण
यदि मान नहीं मिलता है, तो खोज () विधि -1 लौटाती है, लेकिन सूचकांक () विधि एक अपवाद उठाएगी:
txt = "Hello, welcome to my world."
print(txt.find("q"))
print(txt.index("q"))