Is It Bug?

Chris Angelico rosuav at gmail.com
Sun Dec 8 02:05:41 CET 2013


On Sun, Dec 8, 2013 at 11:59 AM, Mahan Marwat <mahanmarwat at gmail.com> wrote:
> Why this is not working.
>
>>>> 'Hello, \\\\World'.replace('\\', '\\')
>
> To me, Python will interpret '\\\\' to '\\'. And the replace method will replace '\\' with '\'. So, the result will be 'Hello, \World'. But it's give me 'Hello, \\\\World'.
>
> The result I want form the code is 'Hello, \World'.

You're replacing with the same as the source string. That's not going
to change anything.

The first thing to get your head around is Python string literals.
You'll find them well described in the online tutorial, or poke around
in the interactive interpreter. Once you master that, you should be
able to understand what you're trying to do here.

ChrisA



More information about the Python-list mailing list