Code wars : Title Case with python
<pre><code>def title_case(title, minor_words = 0):
    title = title.lower().split(" ")
    title_change = []
    temp = []
    if minor_words != 0 :
        minor_words = minor_words.lower().split(" ")
        for i in range(len(title)):
            if (i != 0 and title[i] not in minor_words) or (i == 0 and title[i] in minor_words):
                temp = list(title[i].lower())
                temp[0] = temp[0].upper()
                title_change.append("".join(temp))
            else:
                title_change.append(title[i])
            temp = []
    else:
        for i in range(len(title)):
            temp = list(title[i])
            temp[0] = temp[0].upper()
            title_change.append("".join(temp))
            temp = []
    return " ".join(title_change)
</code></pre>

<p>Hello,this is my python code here.
This is the question:
A string is considered to be in title case if each word in the string is either (a) capitalised (that is, only the first letter of the word is in upper case) or (b) considered to be an exception and put entirely into lower case unless it is the first word, which is always capitalised.</p>

<p>Write a function that will convert a string into title case, given an optional list of exceptions (minor words). The list of minor words will be given as a string with each word separated by a space. Your function should ignore the case of the minor words string -- it should behave in the same way even if the case of the minor word string is changed.</p>

<p>I am trying not to use capitalize() to do this.It seems my code works fine on my computer,but the code wars just prompted "IndexError: list index out of range".</p>

<p>Your code will break if <code>title</code> has leading or trailing spaces, or two consecutive spaces, such as <code>"foo  bar"</code>. It will also break on an empty string. That's because <code>title.lower().split(" ")</code> on any of those kinds of titles will give you an empty string as one of your "words", and then <code>temp[0]</code> will cause an <code>IndexError</code> later on.</p>

<p>You can avoid the issue by using <code>split()</code> with no argument. It will split on any kind of whitespace in any combinations. Multiple spaces will be treated just like one space, and leading or trailing whitespace will be ignored. An empty string will become an empty list when <code>split</code> is called, rather than a list with one empty string in it.</p>

python indexing