Python Forum
python display with '\\' when prints with key-value in dictionary
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python display with '\\' when prints with key-value in dictionary

dict = {
   'name': ""

dict['name'] = "{}".format("\python")
here is the output:
{'name': '\\python'}
The above code snippet, here have created a dictionary of one key and assigned a value to it. When I print the value of dict['name'] it prints '\python', however when it prints with dict and it prints with '\\python'. And this is python 2.

Can any one please help me out to solve this issue. Thanks in advance.
This is not an issue. If you ask for dict['name'] it returns '\python'. If you ask for dict.values() it will return '\python'. Only when you print the dictionary, something you almost only never do except for debugging, will you ever see '\\python'.
Here it is just one key is present in dictionary. Where in the dictionary having the 50/100 keys and sub dictionaries, in that scenario I cannot print each key and its values, instead will print single dictionary at one go as a dict only.

There will face that issue, so how to avoid printing this '\\' when I print whole dictionary itself.
The backslash character has special meaning (escape character), and python is inserting another backslash to say "The next backslash is just a backslash". I do not know why this is necessary, but it must be for some reason. Interestingly python only does this when the next letter is not part of a escape sequence. For example, if I use a value that starts with \n or \r, these do not get the extra backslash. My guess is python is doing you a favor and automatically adding the extra backslash to prevent anyone from treating it as an invalid escape sequence. You should be adding the extra backslash yourself.

It is easy to print out an entire dictionary. And you can use much nicer formatting if you want.

for key, value in dict.items()
    print(key, value)
Just thought of something you could do. Use a raw string. A backslash in a raw string is just a backslash. That must be what Python is doing, saving dictionary keys as raw strings.
This is again printing each keys and value through loop, not in a single go.

but the wonder is why dictionary is adding extra slash to that when it is printing whole dictionary. But when print that key alone it won't add that extra slash.

I know the meaning of escape sequence n all, am a Core C and C++ developer. but just a wonder to know that why python is adding to that and how to avoid printing extra slash.

and mean time I wanted to print dictionary at a single go, not in any loop.

if any one knows, how to print the whole dictionary at one go and without adding extra slash as well.

like for the same below program I want the below output format

dict = {
'name': ""

dict['name'] = "{}".format("\python")

output: {'name': '\python'}

but not as an -- {'name': '\\python'}.

if any one knows, how to avoid to print extra slash, plz do let me know. Thanks in advance.
Has nothing to do with dictionaries. This does the same thing:
value = '\python'

I found this:

"The extra backslash is not actually added; it's just added by the repr() function to indicate that it's a literal backslash. The Python interpreter uses the repr() function (which calls __repr__() on the object) when the result of an expression needs to be printed:"

This makes sense. When you use print(str), python uses str.__str__(). I guess when you use print(dict) python uses __repr__ for the keys and values thinking that you are interested in the details.

I think it is time to write a nice short function that will print your directories. Maybe take a look at pprint or json.dumps.
Cool, response. what you said it is absolutely. However it is same with pprint as well. So let me see some thing different which I can do it my own to print the dictionaries.

The problem, printing can do it any ways, but at the same I also wanted to return the dict to other modules (applications), so how could I do that. Since when return this dict, again the same problem in the application level. So that is where my concern.
The value in the dictionary is '\python'. Anything that uses the value will get '\python'. It is only the annoying print(dict) that calls __repr__ instead of __str__ where we see '\\python', and that is just an artifact of using __repr__ instead of __str__ to get a string to print. I would not worry about passing the dictionary to other modules. You aren't printing the dictionary to get access to the keys and values, are you?
Yes, we are passing this dictionary to another application, and there in that retrieving/accessing the keys, values and printing it. And while printing it display with '\\' instead of '\' as well.
Don't see the problem here as mention bye @deanhystad so do dictionary or any data collection show the repr() values.
That's because dictionaries are not end-user representable data structures,more like debug friendly for developers.
Usually for end-user there is print() of data and then is show \python.

If want to show dictionary for some reason that i don't see why,can make rebuild output it with print().
Don't use dict as a variable name as it's a reserved word in Python.
>>> my_dict = {'name': '\python'}
>>> my_dict
{'name': '\\python'}

>>> print(''.join(f'{{"{k}": "{v}"}}' for k,v in my_dict.items()))
{"name": "\python"}

Possibly Related Threads…
Thread Author Replies Views Last Post
  Output prints at the end? LastStopDEVS 5 531 Dec-19-2020, 05:59 AM
Last Post: buran
Information Unable to display joystick's value from Python onto display box MelfoyGray 2 405 Nov-11-2020, 02:23 AM
Last Post: MelfoyGray
  Try/Exept prints only ones tester_V 11 691 Nov-03-2020, 02:38 AM
Last Post: tester_V
  loop only prints last character. mcmxl22 1 607 Feb-17-2020, 02:36 AM
Last Post: menator01
  How To Display this Python Code in Web browser? pradhan 1 1,358 Jul-14-2019, 06:59 PM
Last Post: snippsat
  Escape sequences display in python Uchikago 1 754 Jun-27-2019, 03:25 PM
Last Post: Gribouillis
  Convert List of Dictionary to dictionary of dictionary list in python kk230689 2 1,290 Apr-27-2019, 03:13 AM
Last Post: perfringo
  can you understand why this code prints None? arcbal 2 1,061 Mar-13-2019, 02:57 AM
Last Post: arcbal
  How to display <IPython.core.display.HTML object>? pythopen 2 29,472 Oct-17-2018, 06:22 PM
Last Post: pythopen
  What for a file that prints nothing sylas 1 954 Sep-12-2018, 01:18 PM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020