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`
6
  • 1
    All that has been posted is a program description. However, we need you to ask a question. 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; asking us to write the program for you, suggestions, and external links are off-topic. Please posst your try to solve this as minimal reproducible example and explain what is wrong with it. Commented Jan 20, 2019 at 10:26
  • @PatrickArtner edited the question. Commented Jan 20, 2019 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. Commented Jan 20, 2019 at 10:45
  • @PatrickArtner I have edited the question. Sorry for the previous misleading one. Commented Jan 20, 2019 at 10:52
  • Maybe you should step back and persue some tutorials on either regex: regex or string-replace: howto use string replace Commented Jan 20, 2019 at 10:56

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.