String Concatenation Clarification in python

Issue

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.

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):

>>> '"'
'"'
>>> '\''
"'"

Answered By – Masklinn

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published