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

    Join Date
    Sep 2012
    Rep Power

    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?


    b = {}

    a = input ("Your text here: ")

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

  2. #2
  3. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Rep Power
    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