#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    66
    Rep Power
    3

    Using string literals as keys


    Hi, so if I have a string returned from the input() function how can I make that string (or part of the string) a key in a dictionary?


    ex:

    b = {}

    a = input ("Your text here: ")

    how would I make the value of a the key in b?

    thanks
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481
    Strings are immutable. Thus they have a hash that won't change. Therefor they can be dictionary keys.

    strings, tuples, frozensets, and um, numbers can be keys. There may be a few other types that can also be used for keys, possibly None, type(object) .

    The python3 value returned from input() can be directly used as a dictionary key.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo