استبدل سلسلة Python () الطريقة
مثال
يستعاض عن كلمة "موز":
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)