Python String index() နည်းလမ်း
ဥပမာ
စာသားထဲမှာ "ကြိုဆိုပါတယ်" ဆိုတဲ့ စကားလုံးက ဘယ်မှာလဲ။
txt = "Hello, welcome to my world."
x = txt.index("welcome")
print(x)
အဓိပ္ပါယ်နှင့် အသုံးပြုမှု
index()
နည်းလမ်းသည် သတ်မှတ်ထားသောတန်ဖိုး၏ ပထမဆုံးဖြစ်ပေါ်မှုကို ရှာဖွေသည် ။
တန်ဖိုးကို ရှာမတွေ့ပါ က index()
နည်းလမ်းသည် ခြွင်းချက်တစ်ခုဖြစ်သည်။
method သည် index()
method နှင့်နီးပါးတူသည်
find()
၊ တစ်ခုတည်းသောကွာခြားချက်မှာ find()
method သည် value ကိုမတွေ့ပါက -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 |
နောက်ထပ် ဥပမာများ
ဥပမာ
စာသားတွင် စာလုံး "e" ၏ ပထမဆုံး ပေါ်ပေါက်မှုသည် အဘယ်မှာရှိသနည်း။
txt = "Hello, welcome to my world."
x = txt.index("e")
print(x)
ဥပမာ
ရာထူး 5 နှင့် 10 ကြားတွင်သာ သင်ရှာဖွေသောအခါတွင် စာလုံး "e" ၏ ပထမဆုံး ဖြစ်ပေါ်လာခြင်းမှာ စာသားတွင် အဘယ်မှာရှိသနည်း။
txt = "Hello, welcome to my world."
x = txt.index("e",
5, 10)
print(x)
ဥပမာ
တန်ဖိုးကို ရှာမတွေ့ပါက find() method သည် -1 သို့ ပြန်ပို့ပေးသော်လည်း index() method သည် ခြွင်းချက်တစ်ခု တိုးလာလိမ့်မည်-
txt = "Hello, welcome to my world."
print(txt.find("q"))
print(txt.index("q"))