By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,931 Members | 1,976 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,931 IT Pros & Developers. It's quick & easy.

replacing \ in python

P: 14
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
Dec 29 '06 #1
Share this Question
Share on Google+
3 Replies


bartonc
Expert 5K+
P: 6,596
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.
Dec 29 '06 #2

bartonc
Expert 5K+
P: 6,596
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'
>>>
Dec 29 '06 #3

Expert 100+
P: 511
Expand|Select|Wrap|Line Numbers
  1. >>> s = "c:\sfsdf"
  2. >>> s
  3. 'c:\\sfsdf'
  4. >>> s.replace("\\","")
  5. 'c:sfsdf'
  6. >>> 
  7.  
Dec 29 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.