Python String Handling

Steve D'Aprano steve+python at pearwood.info
Fri Nov 11 20:41:10 EST 2016


On Sat, 12 Nov 2016 09:29 am, subhabangalore at gmail.com wrote:

> I have a string
> "Hello my name is Richard"
> 
> I have a list of words as,
> ['Hello/Hi','my','name','is','Richard/P']
> 
> I want to identify the match of 'Hello' and 'Richard'
> in list, and replace them with 'Hello/Hi" and 'Richard/P'
> respectively.
> 
> The result should look like,
> "Hello/Hi my name is Richard/P".

Looks like you want:


mystring = "Hello my name is Richard"
words = ['Hello/Hi', 'my', 'name', 'is', 'Richard/P']
result = " ".join(words)

assert result == "Hello/Hi my name is Richard/P"


and mystring is irrelevant.




-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list