Using strip() method and punctuation in Python
<p>I've been trying to solve this problem for a few hours now and can't come with the right solution, this is the question:</p>

<p>Write a loop that creates a new word list, using a
    string method to strip the words from the list created in Problem 3
    of all leading and trailing punctuation. Hint: the string library,
    which is imported above, contains a constant named punctuation.
    Three lines of code.</p>

<p>Here is my code:</p>

<pre><code>    import string

    def litCricFriend(wordList, text):
        theList = text.lower().replace('-', ' ').split()   #problem 3

        #problem below
        for word in theList:
            word.strip(string.punctuation)
            return theList
</code></pre>

<p>You've got a couple bits in your code that... well, I'm not really sure why they're there, to be honest, haha. Let's work through this together!</p>

<p>I'm assuming you have been given some text: <code>text = "My hovercraft is full of eels!"</code>. Let's split this into words, make the words lowercase, and remove all punctuation. We know we need <code>string.punctuation</code> and <code>str.split()</code>, and you've also figured out that <code>str.replace()</code> is useful. So let's use these and get our result!</p>

<pre><code>import string

def remove_punctuation(text):
    # First, let's remove the punctuation.
    # We do this by looping through each punctuation mark in the
    # `string.punctuation` list, and then replacing that mark with
    # the empty string and re-assigning that to the same variable.
    for punc in string.punctuation:
        text = text.replace(punc, '')

    # Now our text is all de-punctuated! So let's make a list of
    # the words, all lowercased, and return it in one go:
    return text.lower().split()
</code></pre>

<p>Looks to me like the function is only three lines, which is what you said you wanted!</p>

<hr>

<p>For the advanced reader, you could also use <code>functools</code> and do it in one line (I split it into two for readability, but it's still "one line"):</p>

<pre><code>import string
import functools

def remove_punctuation(text):
    return functools.reduce(lambda newtext, punc: newtext.replace(punc, ''),
        punctuation, text).lower().split()
</code></pre>

python strip