-2

I am new to python and I am stuck in a situation here! I have stored strings in a python dictionary and when I try to extract it python is wrapping single quotes around the value and giving it back. How can I get rid of the single quotes?

Example code:

myDict = {"name":"Rosy", "class":"9", "likes":"cakes"}
myDict.get("name")

Output: 'Rosy' Desired output: Rosy

raman
  • 93
  • 2
  • 8

3 Answers3

3

I think you probably just want to:

print(myDict.get("name"))

note that this isn't specific to dictionaries. when you're in the Python "REPL" (read-eval-print loop) it will call repr (short for representation) on objects to turn them into something that might be helpful when debugging.

if you're running the script from the command line, lines like you entered don't get displayed and the result gets discarded

Sam Mason
  • 15,216
  • 1
  • 41
  • 60
  • @Austin have added an explanation… – Sam Mason Nov 05 '18 at 18:09
  • Thanks for the quick response Sam. I am trying to feed the output of the dictionary into the `SubElement` method of the `xml.etree.ElementTree`. For example, `child = SubElement(myDict.get(sorted(myDict.keys())[-1]), sorted(myDict.keys())[-1])`, but python is returning type error: – raman Nov 05 '18 at 18:17
  • maybe post a new question with the problem you're actually trying to solve? it's very difficult to read code in comments – Sam Mason Nov 05 '18 at 18:19
1

If I run your code in console, I actually get your desired output. However, try this:

myDict = {"name":"Rosy", "class":"9", "likes":"cakes"}
print(myDict["name"])

PS: If your using something like debug.log() in an IDE, the single quotes are normal, they tell you that it's a string. I hope this is what you asked for.

Tobi696
  • 448
  • 5
  • 16
0

The single quotes is how Python indicates that it's a string. They aren't part of the value. If you print it out, or if you're in Spyder, if you assign the value to a variable and then view the variable in the variable explorer, there won't be quotes.

Acccumulation
  • 3,491
  • 1
  • 8
  • 12