Python String replace() နည်းလမ်း
ဥပမာ
"ငှက်ပျောသီး" ဟူသော စကားလုံးကို အစားထိုးပါ။
txt = "I like bananas"
x = txt.replace("bananas", "apples")
print(x)
အဓိပ္ပါယ်နှင့် အသုံးပြုမှု
replace()
နည်းလမ်းသည် သတ်မှတ်ထားသော စကားစုတစ်ခုကို အခြားသတ်မှတ်ထားသော စကားစုဖြင့် အစားထိုးသည် ။
မှတ်ချက်- အခြားမည်သည့်အရာကိုမျှ မဖော်ပြထားပါက သတ်မှတ်ထားသော စကားစုများ၏ ဖြစ်ပျက်မှု အားလုံးကို အစားထိုးပါမည်။
အထားအသို
string.replace(oldvalue, newvalue, count)
ကန့်သတ်တန်ဖိုးများ
Parameter | Description |
---|---|
oldvalue | Required. The string to search for |
newvalue | Required. The string to replace the old value with |
count | Optional. A number specifying how many occurrences of the old value you want to replace. Default is all occurrences |
နောက်ထပ် ဥပမာများ
ဥပမာ
"တစ်ခုတည်း" ဟူသော စကားလုံး၏ ဖြစ်ပေါ်မှုအားလုံးကို အစားထိုးပါ။
txt = "one one was a race horse, two two was one too."
x =
txt.replace("one", "three")
print(x)
ဥပမာ
"one" ဟူသော စကားလုံး၏ ပထမဆုံး ဖြစ်ပေါ်မှု နှစ်ခုကို အစားထိုးပါ။
txt = "one one was a race horse, two two was one too."
x =
txt.replace("one", "three", 2)
print(x)