0

Let's assume I have a string:

a="qihdkasf\sdgbsdf\rgsdg"

I want to replace "\" in string a with "/" in python. I do know that while printing "\", we need to write it as print("\\"). Considering that, I tried doing something like this, as shown below:

a.replace("\\","/")

but, it doesn't seem to be working!

Any help on the matter will be really appreciated.

Saurav Saha
  • 745
  • 1
  • 11
  • 30
  • Don't seem working : explain, always ;) Also you have not a *\\* and a *r* which is a *\r* a special char, you cant replace the single \ – azro Mar 24 '20 at 08:19
  • 2
    well, did you really define `a="qihdkasf\sdgbsdf\rgsdg"` as you've shown? because the issue is, `\r` has a meaning, so that became a character. also, this code *is* working fine for what it's suppsoed to do – Paritosh Singh Mar 24 '20 at 08:19
  • 3
    Also, i bet [this](https://stackoverflow.com/questions/9189172/why-doesnt-calling-a-python-string-method-do-anything-unless-you-assign-its-out) is your issue – Paritosh Singh Mar 24 '20 at 08:21
  • 1
    @ParitoshSingh, Yup! The thread that you posted, solved the issue! Thank you so much! I am pretty careless and I shouldn't have made such a rookie mistake :P – Saurav Saha Mar 24 '20 at 08:26

1 Answers1

3

you can use:

r"qihdkasf\sdgbsdf\rgsdg".replace('\\', '/')

output:

qihdkasf/sdgbsdf/rgsdg
kederrac
  • 16,819
  • 6
  • 32
  • 55