Python - Need to extract the first and last letter from a word string
<p>I've been stuck with this for a while. basically the <code>get_text_value(text)</code> code below is passed a string of words and it extracts the first and last letter of each word, matches it up with <code>letter_values</code> and gives the final value based on the sum of the numbers the first and last words matched up with. So far I've only been able to "extract" the first and last letter of the whole string and not each individual word. 
I know I'm meant to convert the string into a list, but I'm not entirely sure how to go about it. Any help would be greatly appreciated. Thanks!</p>

<pre><code>def get_text_value(text):
     letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
     letter_values = [1, 4, 2, 3, 1, 4, 3, 4, 1, 7, 7, 4, 6, 6, 1, 3, 9, 2, 2, 2, 1, 8, 5, 9, 9, 9]
     text_value = 0
     text = text.lower()
     for i in text:
          if text[0] and text[-1] in letters:
               letters_index_1 = letters.index(text[0])
               letters_index_2 = letters.index(text[-1])
               text_value = letter_values[letters_index_1] + letter_values[letters_index_2]

     return text_value

def test_get_text_value():
     print("1. Text value:", get_text_value("abracadabra")) 
     print("2. Text value:", get_text_value("a b"))
     print("3. Text value:", get_text_value("enjoy Today"))
     print("4. Text value:", get_text_value(""))

     text = "Be yourself everyone else is already taken"
     text_value = get_text_value(text)
     print("6. Text:", text)
     print("6. Text value:", text_value)
</code></pre>

<p>Looks like you're just getting started with Python. Welcome! Couple things that might be worth getting more familiar with.</p>

<ol>
<li>Dictionaries</li>
</ol>

<p>Dictionaries are good for storing a key value pair. In your case, a dictionary would work really well as you have a letter (your key) and a numerical value (the value). As mentioned in the other answer, a dictionary is going to significantly clean up your code.</p>

<ol start="2">
<li>String methods.</li>
</ol>

<p>The function you need to go from getting the first and last letter of the entire sentence to the first and last of each word is <code>split</code>. It will split one string into a list of strings where it finds a space.</p>

<pre><code>&gt;&gt;&gt; "enjoy Today".split(" ")
['enjoy', 'Today']
&gt;&gt;&gt; "abracadabra".split(" ")
['abracadabra']
</code></pre>

<p>You don't have to just split on spaces.</p>

<pre><code>&gt;&gt;&gt; "abracadabra".split("b")
['a', 'racada', 'ra']
</code></pre>

<ol start="3">
<li>List comprehension</li>
</ol>

<p>Just google it as there are lots of articles that will do a better job explaining then I will here.</p>

<p>These 3 methods are a few of the key building blocks that will make Python enjoyable to write and your code concise and easy to read.</p>

python string list python-3.x