3

In Python, I have a string:

a = "\s"

In JavaScript, a would be the single letter "s", but in Python, a would be "\s".

How can I make Python behave the same way as JavaScript in this situation?


the real case may be more complicate : a = "<div class=\"haha\"><\/div>" , In this case , JavaScript get right HTML but python failed
eliriclzj
  • 115
  • 6
  • `a="s"`. There is some differences between languages. Your string is `\s` if you want an output like `s` then you have to change it. Otherwise I don't see anything wrong here. Or use regex . – GLHF Jan 23 '15 at 03:01
  • Do you mean a = "<\div>"? (ack... you're editing while I ask, so feel free to ignore this) – Foon Jan 23 '15 at 03:08
  • 1
    This is just a language-based quirk. Don't use unintuitive escapes and you won't run into this problem. Your "real-case" demonstrates this. – simonzack Jan 23 '15 at 03:32

1 Answers1

1

Assuming that there are no encoding/decoding that is happening?

Is a == r"\s" ?

You could simply:

a.replace('\\','')

example:

>>> a = "<div class=\"haha\"><\/div>"
>>> a.replace('\\','')
'<div class="haha"></div>'

See:

Community
  • 1
  • 1
jmunsch
  • 22,771
  • 11
  • 93
  • 114