-1

Why does:

B = "The" + "\s"

and

B = "The" + r"\s"

yield:

"The\\s"

Is it possible to write the above, such that the output string is:

"The\s"

I have read similar questions on both the issue of backslashes, and their property for escaping, and the interpretation of regex characters in Python.

How to print backslash with Python?

Why can't Python's raw string literals end with a single backslash?

Does this mean there is no way to write what I want?

If it is useful, My end goal is to a write a program that adds the regex expression for space (\s) to a string where this such space:

For example, start with:

A = "The Cat and Dog"

After applying the function, this becomes:

B = "The\sCat\sand\sDog"
Community
  • 1
  • 1
Chuck
  • 3,664
  • 7
  • 42
  • 76

3 Answers3

2

I believe this is related to Why does printing a tuple (list, dict, etc.) in Python double the backslashes?

The representation of the string and what it actually contains can differ.

Observe:

>>> B = "The" + "\s"
>>> B
'The\\s'
>>> print B
The\s

Furthermore

>>> A = "The Cat and Dog"
>>> B = str.replace(A, ' ', '\s')
>>> B
'The\\sCat\\sand\\sDog'
>>> print B
The\sCat\sand\sDog
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
doctorlove
  • 18,872
  • 2
  • 46
  • 62
  • 1
    You forgot the closing parenthesis on `B = str.replace(A, ' ', '\s'`. OTOH, `B = A.replace(' ', '\s')` is more idiomatic, although I'd probably write it as `B = A.replace(' ', '\\s')` or `B = A.replace(' ', r'\s')` to make it explicit that the replacement string contains two chars - a backslash and a `'s'`. – PM 2Ring Dec 01 '16 at 11:59
  • 1
    Oops... I'll correct the closing brace, but yes your replaces are better – doctorlove Dec 01 '16 at 12:01
  • Thank you for your help in highlighting the difference between what you write in the string, and how Python interprets the string. It appears I can do exactly the search I need with this information. Thank you :) – Chuck Dec 01 '16 at 12:12
2

From the docs:

all unrecognized escape sequences are left in the string unchanged, i.e., the backslash is left in the result

So while \s is not a proper escape sequence, Python forgives you your mistake and treats the backslash as if you had properly escaped it as \\. But when you then view the string's representation, it shows the backslash properly escaped. That said, the string only contains one backslash. It's only the representation that shows it as an escape sequence with two.

Stefan Pochmann
  • 27,593
  • 8
  • 44
  • 107
1

You must escape the "\"

B = "The" + "\\s"

>>> B = "The" + "\\s"
>>> print(B)
The\s

See the Escape Sequences part: Python 3 - Lexical Analysis

emKaroly
  • 756
  • 1
  • 10
  • 22
  • That still yields: "The\\s" – Chuck Dec 01 '16 at 11:46
  • 1
    Try it in IDLE you will see. – emKaroly Dec 01 '16 at 11:48
  • I see what you mean... Explicitly using print changes the output, presumably as it reads the string literal? – Chuck Dec 01 '16 at 11:51
  • @CharlesMorris check the link i added to my answer, it explains the Escape Sequences – emKaroly Dec 01 '16 at 11:56
  • 2
    @emKaroly Python 2.0 from over 15 years ago is quite obsolete, I wouldn't link to that. – Stefan Pochmann Dec 01 '16 at 11:59
  • 2
    As Stefan Pochmann explains, it's _not_ necessary here to escape the backslash, since `\s` isn't a Python string escape sequence (although it _is_ a Python 're' module regex escape sequence). But it is clearer to either escape it or to use a raw string. – PM 2Ring Dec 01 '16 at 12:05
  • Thanks for the help - it looks like a lack of understanding of string escaping + not realising that A print A will give you different outputs when viewed in Shell vs Standard Output, was the root of my problem. Thanks for info :) – Chuck Dec 01 '16 at 12:07