Replacing a space with \
Hello. I've been really struggling with this one and can't for the life of me solve it.
I have a string, let's say: Hello there and I need the space to be replaces with \ (slash space) but the problem I'm facing is that if I'm replacing with that, python will introduce another slash to escape it but I only need one slash, not two. The final result should be Hello\ there and not Hello\\ there. Can anyone enlighten me ?
Posted Mar 19, 2020, 3:10 PM UTC
Try printing it. I doubt there's actually two
\.This is what I get
https://imgur.com/a/3uX0CNo
Edit: Thanks for teaching me something new. I had no idea that if the value is not stored on a variable, there is additional escaping.
Read carefully what he said
And the last statement in his code
he indicates to use
print()as that prevents the addition of extra escaping slashes.Because you're not printing it. It's falling back to string's
repr, which escapes characters. Example:If you're still convinced that your string is wrong and not just a display issue, try taking the
lenand you'll see that the second\isn't being counted because it's not actually in the string.