-5

When I create a string containing backslashes, they get duplicated:

NOTE : I want to add \ in request because i want to call third party API and they want me to send request with \ in some of their keys.

I have taken reference from this answer Why do backslashes appear twice?, but its working only for string, not for dict.

mystr = {"str": "why\does\it\happen?"}
print(mystr)

output:
{'str': 'why\\does\\it\\happen?'}

here i am attching a screenshot for better understanding. enter image description here

Ketan Modi
  • 1,780
  • 1
  • 16
  • 28
  • 2
    This has little to do with the `dict`. It's how Python displays a string that contains a literal backslash, because it also uses backslashes to display things like tabs (`\t`) and carriage returns (`\r`). – chepner Apr 25 '22 at 13:54
  • that should be `{"str": r"why\does\it\happen?"}` - else you may have a string with "\nothing" and it resolves to newLine+"othing" - the r"..." tells pyhton to treat the string as raw string and \ as literals instead of special chars. - lternatively you can use `{"str": "why\\does\\it\\happen?"}` yourself to mark every \ as a literal \\ – Patrick Artner Apr 25 '22 at 13:55
  • 2
    The `dict` only comes into play because `dict.__str__` (used by `print`) uses `str.__str__` to display the values in the `dict`. – chepner Apr 25 '22 at 13:55
  • 1
    "its working only for string, not for dict" Can you clarify what you mean by that? Note that you have linked to a question, not an answer. – MisterMiyagi Apr 25 '22 at 13:58
  • Does this answer your question? [Why do backslashes appear twice?](https://stackoverflow.com/questions/24085680/why-do-backslashes-appear-twice) – luk2302 Apr 25 '22 at 13:59
  • No, it isn't anwer my question @luk2302 – Ketan Modi Apr 25 '22 at 14:01
  • 1
    It does answer your question, tired of debating this. Unless you get clear what you would expect and why this post is just noise. – luk2302 Apr 25 '22 at 14:02

2 Answers2

1

mystr isn't a str, it's a dict. When you print a dict, it prints the repr of each string inside it, rather than the string itself.

>>> mydict = {"str": "why\does\it\happen?"}
>>> print(mydict)
{'str': 'why\\does\\it\\happen?'}
>>> print(repr(mydict['str']))
'why\\does\\it\\happen?'
>>> print(mydict['str'])
why\does\it\happen?

Note that the repr() includes elements other than the string contents:

  1. The quotes around it (indicating that it's a string)
  2. The contents use backslash-escapes to disambiguate the individual characters. This extends to other "special" characters as well; for example, if this were a multiline string, the repr would show linebreaks as \n within a single line of text. Actual backslash characters are always rendered as \\ so that they can be distinguished from backslashes that are part of other escape sequences.

The key thing to understand is that these extra elements are just the way that the dict is rendered when it is printed. The actual contents of the string inside the dict do not have "doubled backslashes", as you can see when you print mydict['str'].

If you are using this dict to call an API, you should not be using str(mydict) or anything similar; if it's a Python API, you should be able to use mydict itself, and if it's a web API, it should be using something like JSON encoding (json.dumps(mydict)).

Samwise
  • 68,105
  • 3
  • 30
  • 44
0

I think that to build the printed string of the dict, python call the __repr__ method of object inside it (for the values) instead of the __str__ as you would expect for printing the dict.

It would make sense since dict can contain every type of object not just string so the __repr__| method can be found everywhere (it's included in the base object in python) when the __str__ need to be written.

But it's only a guess, not a definitive answer.

Xiidref
  • 1,456
  • 8
  • 20