Adding inverted commas to a value [on hold]


Adding inverted commas to a value [on hold]



So I am receiving a value through my code and I need to use this value as a key in a dictionary and therefore I need to put this value between inverted commas.



How can I do that?



Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.





If you're asking "how do I make an object into a string so I can use it as a key in my dictionary?", are you sure you actually need to make it into a string? Many kinds of objects are capable of being keys.
– Kevin
Jun 29 at 18:12





What is an inverted comma? Do you mean a single quote ' character?
– martineau
Jun 29 at 18:13



'





@martineau Wiki: en.wiktionary.org/wiki/inverted_comma
– Anton vBR
Jun 29 at 18:14





dict[string_var] = value_to_be_assigned
– Bhavya Jain
Jun 29 at 18:15






OK, why do you think you need "... to put this value between inverted commas"? Please add some sample code or something to your question that illustrates what you want to do.
– martineau
Jun 29 at 18:18





1 Answer
1



It seems like you want to convert a value to string:


x = 5
str_x = str(x)



Then if you want to use it as dict value, do:


mydict[str_x] = some_item



But there's no reason to do something like that. Anything can be used as dict key, so you can use the value itself.



In fact you can't use dicts, sets as a dict keys.





No, "anything" cannot be used as a dictionary key, it needs to be immutable and hashable. You cannot use a list as a dict key, but you could use a tuple
– roganjosh
Jun 29 at 18:32






It looks like really can't dicts, sets, lists, but you can use objects.
– C05
Jun 29 at 18:39






Everything is an object in python. I already made the distinction of what can be used for you. "Anything can be used as dict key" is simply incorrect.
– roganjosh
Jun 29 at 18:45


Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Export result set on Dbeaver to CSV

Opening a url is failing in Swift