Python String Handling

Thomas Nyberg tomuxiong at gmx.com
Fri Nov 11 17:44:24 EST 2016


On 11/11/2016 05:29 PM, subhabangalore at gmail.com wrote:
> I have a string
> "Hello my name is Richard"
>
> 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".
>
> Simple replace method may not work.
>
Why would simple replace not work? I.e. what's wrong with this?

s = '"Hello my name is Richard"'
s.replace("Hello", "Hello/Hi").replace("Richard", "Richard/P")

I mean maybe you can't use that, but if you can't use that, then I'm 
probably not understanding your question.

Cheers,
Thomas



More information about the Python-list mailing list