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

3
  • 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 . Commented Jan 23, 2015 at 3:01
  • Do you mean a = "<\div>"? (ack... you're editing while I ask, so feel free to ignore this) Commented Jan 23, 2015 at 3: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. Commented Jan 23, 2015 at 3:32

1 Answer 1

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:

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.