How to print specific value from key in a dictionary?
<p>Our teacher set us a challenge to make a program that will allow users to input a symbol of an element and the program should output some info about the element.</p>

<p>To do this I have to use dictionaries. Currently I have this:</p>

<pre><code>elements = {"Li": "Lithium" " 12" " Alkali Metal"}
element = input("Enter an elemental symbol: ")
print (elements[element])
</code></pre>

<p>This prints everything that is related to Li.</p>

<p>I was wondering how I would be able to only output, say Alkali Metal, rather than everything associated with Li? (Yes I know 12 isn't Lithium's atomic number)</p>

<p>You currently have one string as a value so there is not much you can do reliably. You would need to store separate values which you could do with a sub-dict:</p>

<pre><code>elements = {"Li": {"full_name":"Lithium", "num":"12", "type":"Alkali Metal"}}
</code></pre>

<p>Then just access the nested dict using the key of what particular value you want to get:</p>

<pre><code>In [1]: elements = {"Li": {"full_name":"Lithium", "num":"12", "type":"Alkali Metal"}}

In [2]: elements["Li"]["num"]
Out[2]: '12'

In [3]: elements["Li"]["full_name"]
Out[3]: 'Lithium'

In [4]: elements["Li"]["type"]
Out[4]: 'Alkali Metal'
</code></pre>

<p>If you have strings with no comma separating each substring,  python will  create a single string:</p>

<pre><code>In [5]: "Lithium" " 12" " Alkali Metal"
Out[5]: 'Lithium 12 Alkali Metal'
In [6]: "Lithium","12","Alkali Metal"
Out[6]: ('Lithium', '12', 'Alkali Metal') # now its a three element tuple
</code></pre>

python python-3.x