How to Load a String Into a Python Dictionary

Python has a number of different data types including strings, tuples and dictionaries. A dictionary contains multiple key value pairs. For example, if you were keeping a list of lunch orders for the office each co-worker would be a "key" and his lunch choice would be the corresponding "value." A Python dictionary of this information might look like: { john: 'sandwich', margaret: 'salad', josie: 'fruit' }.

Instructions

    • 1

      Open the file containing the Python code you want to execute or a Python interpreter.

    • 2

      Type the following to create a new dictionary if it does not already exist:

      lunchorders = {}

    • 3

      Type the following to add a string to the dictionary:

      lunchorders['key'] = string

      Replace "key" with the specific key for this string. Replace "string" with your string whether it is a hard-coded literal string or a variable representing a string.

Related Searches:

References

Comments

Related Ads

Featured