0

If there is an example phrase like,

A guy named Rajesh Kumar Singh came to meet me yesterday.

I have to remove the name of "Secret agent" from this string, transforming the string like this:

A guy named XXXX came to meet me yesterday.

I have the logic of identifying the name in string, I am stuck at the string manipulation, to remove word number 4,5,6 from the original string and replacing it with XXXX.

2
  • Would the input always start with A guy named <some name>, or are there other variations? Also, can you include your current Python script in your question? Commented May 24, 2021 at 10:25
  • No, but the logic of finding the name is well in place. As mentioned in the problem, i need help with string manipulation. Commented May 24, 2021 at 10:27

2 Answers 2

1
Once we know the name to be replaced, then using replace method we can make it 
   
userText = "A guy named Rajesh Kumar Singh came to meet me yesterday."
name = "Rajesh Kumar Singh"
replacedName = "XXXXX YYYY ZZ"
userText.replace(name, replacedName)
'A guy named XXXXX YYYY ZZ came to meet me yesterday.'
Sign up to request clarification or add additional context in comments.

Comments

1

Assuming the text would always start with the phrase A guy named <some name>, we can try using re.sub here as follows:

inp = "A guy named Rajesh Kumar Singh came to meet me yesterday."
output = re.sub(r'^A guy named(?: [A-Z][a-z]*)+', 'A guy named XXXX', inp)
print(output)  # A guy named XXXX came to meet me yesterday.

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.