Reputation: 59
I have two strings so for example:
string1 = "abcdefga"
string2 = "acd"
I need to make string one return with "befga"
I can replace it but if string1
has two of the same characters it gets rid of both so for example my string1
keeps returning as "befg
":
for char in string1:
for nike in string2:
if char == nike:
string1 = string1.replace(char,"")
Upvotes: 0
Views: 4195
Reputation: 180917
You can use the maxreplace
parameter of replace
to replace only the first occurrence;
string.replace(s, old, new[, maxreplace])
Return a copy of string s with all occurrences of substring old replaced by new. If the optional argument maxreplace is given, the first maxreplace occurrences are replaced.
string1 = "abcdefga"
string2 = "acd"
for ch in string2:
string1 = string1.replace(ch, '', 1)
print(string1)
'befga'
Upvotes: 7