पायथन स्ट्रिंग की जगह () विधि
उदाहरण
"केले" शब्द को बदलें:
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)
उदाहरण
शब्द "एक" की पहली दो घटनाओं को बदलें:
txt = "one one was a race horse, two two was one too."
x =
txt.replace("one", "three", 2)
print(x)