4

I am new to python and I want to replace same substring occurrences of a particular string with different substrings by using python. I already tried the .replace() function of python, but it replaces all the occurrences with the new substring. Example for the question is below.


string = "I am a student as well as a teacher" Here I want to replace the substring "as" with "xas" and "yas" by adding extra character to the substring. The final result should be "I am a student xas well yas a teacher"


Code that I have tried:

string = "I am a student as well as a teacher"
 occurrences = re.findall("as", string)
 substr = ["xas","yas"]
 i = 0
 for occur in occurrences:
     string = string.replace(occur, substr[i])
     i = i + 1`
  • 1
    All that has been posted is a program description. However, we need you to [ask a question](//stackoverflow.com/help/how-to-ask). We can't be sure what you want from us. Please [edit] your post to include a valid question that we can answer. Reminder: make sure you know [what is on-topic here](//stackoverflow.com/help/on-topic); asking us to write the program for you, suggestions, and external links are off-topic. Please posst your try to solve this as [mcve] and explain what is wrong with it. – Patrick Artner Jan 20 '19 at 10:26
  • @PatrickArtner edited the question. – Shan Chathusanda Jayathilaka Jan 20 '19 at 10:38
  • now you use regex to find something and string.replace to replace it ... you would need re.sub() to replace with regex - don't mix them. – Patrick Artner Jan 20 '19 at 10:45
  • @PatrickArtner I have edited the question. Sorry for the previous misleading one. – Shan Chathusanda Jayathilaka Jan 20 '19 at 10:52
  • Maybe you should step back and persue some tutorials on either regex: [regex](https://docs.python.org/3/howto/regex.html) or string-replace: [howto use string replace](https://stackoverflow.com/questions/9452108/how-to-use-string-replace-in-python-3-x) – Patrick Artner Jan 20 '19 at 10:56
  • @PatrickArtner this is the result I got from the above code segment ---> "I am a student xyas well xyas a teacher" – Shan Chathusanda Jayathilaka Jan 20 '19 at 10:58

2 Answers2

2

You can use regex as well:

substr = ["xxx","yyy"]

def replace_with(_):
    """Returns first value of substr and removes it."""
    return substr.pop(0)

import re

string = "I am a student as well as a teacher"

print(re.sub("as",replace_with,string)) 

Output:

I am a student xxx well yyy a teacher

But the solution using str.replace() with a limit of 1 by Daweo is more elegant.

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
1

You can inform replace how many times it should replace following way

s = "I am a student as well as a teacher"
s = s.replace("as","xxx",1)
print(s) #I am a student xxx well as a teacher
s = s.replace("as","yyy",1)
print(s) #I am a student xxx well yyy a teacher

EDIT: Replacing first as with xas and second as with yas requires different approach

s = "I am a student as well as a teacher"
repl = ["xas","yas"]
s = s.split("as")
for i in repl:
    s = [i.join(s[:2])]+s[2:]
s = s[0]
print(s) #I am a student xas well yas a teacher

Note that this solution assumes number of elements of repl is exatcly equal to number of as in s.

Daweo
  • 31,313
  • 3
  • 12
  • 25