In Python, how can you write the string String = "s"?
Question:
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:
"Thes"
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 = "ThesCatsandsDog"
Answers:
You must escape the ""
B = "The" + "\s"
>>> B = "The" + "\s"
>>> print(B)
Thes
See the Escape Sequences part:
Python 3 – Lexical Analysis
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
Thes
Furthermore
>>> A = "The Cat and Dog"
>>> B = str.replace(A, ' ', 's')
>>> B
'The\sCat\sand\sDog'
>>> print B
ThesCatsandsDog
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.
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:
"Thes"
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 = "ThesCatsandsDog"
You must escape the ""
B = "The" + "\s"
>>> B = "The" + "\s"
>>> print(B)
Thes
See the Escape Sequences part:
Python 3 – Lexical Analysis
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
Thes
Furthermore
>>> A = "The Cat and Dog"
>>> B = str.replace(A, ' ', 's')
>>> B
'The\sCat\sand\sDog'
>>> print B
ThesCatsandsDog
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.