replacing \ in python

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • learnerofpython
    New Member
    • Nov 2006
    • 14

    replacing \ in python

    hi!
    how can i replace a string containing \ character with another chracter.
    The replace function is not working as desired.
    please help me out
    regards,
    LearnerOfPython
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by learnerofpython
    hi!
    how can i replace a string containing \ character with another chracter.
    The replace function is not working as desired.
    please help me out
    regards,
    LearnerOfPython
    Please show us what you are attempting so that we may assist you. Thanks.

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by learnerofpython
      hi!
      how can i replace a string containing \ character with another chracter.
      The replace function is not working as desired.
      please help me out
      regards,
      LearnerOfPython
      Use raw strings when working with the backward slash:

      >>> s = r"C:\local\test \new"
      >>> s.replace("\\", ".")
      'C:.local.test. new'
      >>>

      Comment

      • ghostdog74
        Recognized Expert Contributor
        • Apr 2006
        • 511

        #4
        Code:
        >>> s = "c:\sfsdf"
        >>> s
        'c:\\sfsdf'
        >>> s.replace("\\","")
        'c:sfsdf'
        >>>

        Comment

        Working...