The command
str="str1",'str2'
creates a tuple in the variable str
, and is equivalent to doing
str = ('str1', 'str2')
Note that this is probably the underlying cause of your confusion… you are creating a tuple but putting it in a variable with a name that indicates you expected a string.
When you do
str1 = 'str1'
str2 = 'str2'
dict.get(str1+"','"+str2)
# The above is equivalent to
# mystr = str1+"','"+str2
# dict.get(mystr)
you are trying to access the key "str1','str2"
, which is not in the dictionary. You want to access the tuple ('str1', 'str2')
.
To do this, try
str1 = 'str1'
str2 = 'str2'
mytuple = str1, str2 # or mytuple = (str1, str2)
dict.get(mytuple)
# or dict.get((str1, str2)) # Note the extra parentheses
I am obliged to say that str
and dict
are bad names for a variables because they shadow the python builtin functions str
and dict
.
1
solved Python 2.7: Accessing value from dictionary using string [closed]