Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

String Concatenation Clarification in python

Why does the string concatenation operation ‘A’+’\”+’B’+’\\’+’C’ produce the string ‘A’B\\C’ instead of a single backslash. How to fix this?

And I don’t want to make use of the print function to generate this pattern. I want this to be printed in the >>> prompt.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Why does the string concatenation operation ‘A’+”’+’B’+’\’+’C’ produce the string ‘A’B\C’ instead of a single backslash. How to fix this?

There’s nothing to fix.

And I don’t want to make use of the print function to generate this pattern. I want this to be printed in the >>> prompt.

That’s nonsensical and not possible.

The python console shows the representation (repr) of the object. The repr of a string always escapes backslashes (and includes delimiter quotes) such that it can be pasted as-is as a string literal.

That is also why it changes the delimiter quotes depending on what quote style is embedded in the string (if any):

>>> '"'
'"'
>>> '\''
"'"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading