Python String replace() Method
Εισήγηση
Αντικατάσταση του λέγματος "bananas":
txt = "I like bananas" x = txt.replace("bananas", "apples") print(x)
Definition and Usage
The replace() method replaces a specified phrase with another specified phrase.
Note:If nothing else is specified, it will replace all occurrences of the specified phrase.
Syntax
string.replace(oldvalue, newvalue, count)
Parameter Value
Parameter | Description |
---|---|
oldvalue | Required. The string to be searched for. |
newvalue | Required. The string to be replaced with the old value. |
count | Οptional. Αριθμός, καθορίζει τον αριθμό των εμφανίσεων του παλιού τιμή που θα αντικατασταθεί. Η προεπιλεγμένη τιμή είναι όλες οι εμφανίσεις. |
Περισσότερες Εισήγηση
Εισήγηση
Αντικατάσταση όλων των εμφανίσεων του λέγματος "one":
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)