Is It Bug?

Discussion in 'Python' started by Mahan Marwat, Dec 8, 2013.

  1. Mahan Marwat

    Mahan Marwat Guest

    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'.
     
    Mahan Marwat, Dec 8, 2013
    #1
    1. Advertising

  2. Mahan Marwat

    Iuri Guest

    This way, it will replace '\' with '\', so nothing change.

    >>> 'Hello, \\\\World'.replace('\\\\', '\\')

    'Hello, \\World'
    >>> print 'Hello, \\\\World'.replace('\\\\', '\\')

    Hello, \World


    On Sat, Dec 7, 2013 at 10:58 PM, Mahan Marwat <> 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'.
    > --
    > https://mail.python.org/mailman/listinfo/python-list
    >
     
    Iuri, Dec 8, 2013
    #2
    1. Advertising

  3. Mahan Marwat

    rusi Guest

    On Sunday, December 8, 2013 6:28:24 AM UTC+5:30, Mahan Marwat 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'.


    I am mystified by this question.
    Yes '\\' may be one slash or two or something else more exotic.
    But whatever it is (and its not a syntax error like with '\') it is something.
    So how can ....replace(something,something).... be anything other
    than a no-op?
     
    rusi, Dec 8, 2013
    #3
  4. If I'm having to deal with incessant backslashes in a string I'll often
    use the r'\' (raw string literal) syntax. Simplifies things quite a bit.
     
    Joshua Gardner, Dec 8, 2013
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John

    Re: BUG? OR NOT A BUG?

    John, Sep 20, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    552
  2. RedEye
    Replies:
    2
    Views:
    587
    Jason Kester
    Dec 13, 2005
  3. Michel Joly de Lotbiniere

    Bug Parade Bug 4953793

    Michel Joly de Lotbiniere, Nov 30, 2003, in forum: Java
    Replies:
    4
    Views:
    648
    Michel
    Dec 2, 2003
  4. DarkSpy
    Replies:
    4
    Views:
    893
    tom_usenet
    Jun 27, 2003
  5. Steve Holden
    Replies:
    1
    Views:
    400
    Behrang Dadsetan
    Jul 2, 2003
Loading...

Share This Page