Python String replace() Yöntemi
Örnek
"Muz" kelimesini değiştirin:
txt = "I like bananas"
x = txt.replace("bananas", "apples")
print(x)
Tanım ve Kullanım
Yöntem replace()
, belirtilen bir ifadeyi, belirtilen başka bir ifadeyle değiştirir.
Not: Başka bir şey belirtilmemişse, belirtilen ifadenin tüm tekrarları değiştirilecektir.
Sözdizimi
string.replace(oldvalue, newvalue, count)
Parametre Değerleri
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 |
Daha fazla örnek
Örnek
"Bir" kelimesinin tüm oluşumunu değiştirin:
txt = "one one was a race horse, two two was one too."
x =
txt.replace("one", "three")
print(x)
Örnek
"Bir" kelimesinin ilk iki yerini değiştirin:
txt = "one one was a race horse, two two was one too."
x =
txt.replace("one", "three", 2)
print(x)