简体   繁体   中英

remove quotes from output of python dictionaries

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

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM