You have seen the replace function in Python. It takes two arguments: oldphrase
and newphrase
:
a = "a/b/c/a/b/c"
print(a.replace("a/", ""))
The method operates on string a
and replaces all occurrences of a/
with empty string ""
.
Output:
b/c/b/c
To replace the first occurrence only, add a third argument (count) with value 1
.
a = "a/b/c/a/b/c"
print(a.replace("a/", "", 1))
Output:
b/c/a/b/c
As you see, the first occurrence of a/
has been removed. The second occurrence is still in the result.