Does particular string match strings in text file
<p>I have a text file containing many words (single word on each line). I have to read in each word, modify the words, and then check if the modified word matches any of the words in the file. I am having trouble with the last part (it is the hasMatch method in my code). It sounds simple enough and I know what I should do, but whatever I try does not work.</p>

<pre><code>#read in textfile 
myFile = open('good_words.txt')


#function to remove first and last character in string, and reverse string
def modifyString(str):
    rmFirstLast = str[1:len(str)-2] #slicing first and last char
    reverseStr = rmFirstLast[::-1] #reverse string 
    return reverseStr

#go through list of words to determine if any string match modified string
def hasMatch(modifiedStr):
    for line in myFile:
        if line == modifiedStr:
            print(modifiedStr + " found")
        else:
            print(modifiedStr + "not found")

for line in myFile:
    word = str(line) #save string in line to a variable

    #only modify strings that are greater than length 3
    if len(word) &gt;= 4:
        #global modifiedStr #make variable global
        modifiedStr = modifyString(word) #do string modification
        hasMatch(modifiedStr)

myFile.close()
</code></pre>

<p>In your code, you're not slicing just the first and last character but the first and last two characters.</p>

<pre><code>rmFirstLast = str[1:len(str)-2] 
</code></pre>

<p>Change that to:</p>

<pre><code>rmFirstLast = str[1:len(str)-1] 
</code></pre>

python string python-2.7