Replacing string in a list of strings
<p>I have problem with my code. In this example it should print out <code>The Wind in the Willows</code>, but it does print <strong>The Wind In The Willows</strong>. I think the problem is that replace function does not execute. I have no idea what's wrong with this code. Please help.</p>

<p>PS. Basic idea of this function is to return title lookalike string with exception (<code>minor_words</code>). <code>Minor_words</code> should be lower case in a title (despite the case if <code>minor_words</code> is the first word in the <code>title</code>)</p>

<pre><code>def title_case(title, minor_words):
    exc = [x for x in title.lower().split() if x in minor_words.lower().split()]
    for string in exc:
        if title.split().index(string) == 0:
            title = title.title()
        else:
            title = title.title().replace(string, string.lower())
    return title


print (title_case('THE WIND IN THE WILLOWS', 'The In'))
</code></pre>

<pre><code>def title_case(title, minor_words):
    # lowercase minor_words and make a set for quicker lookups
    minor_set = set(i for i in minor_words.lower().split())
    # tokenize the title by lowercasing
    tokens = title.lower().split()
    # create a new title by capitalizing words that dont belong to minor_set
    new_title = ' '.join(i if i in minor_set else i.capitalize() for i in tokens)
    # Finally return the capitalized title.
    if len(new_title) &gt; 1:
        return new_title[0].upper() + new_title[1:]
    else:
        # Since its just one char, just uppercase it and return it
        return new_title.upper()
</code></pre>

<p>Output:</p>

<pre><code>&gt;&gt;&gt; print (title_case('THE WIND IN THE WILLOWS', 'The In'))
The Wind in the Willows
</code></pre>

python string