Skip to content

Instantly share code, notes, and snippets.

@accessnash
Created October 19, 2015 07:19
Show Gist options
  • Save accessnash/63c63bb54b079667e784 to your computer and use it in GitHub Desktop.
Save accessnash/63c63bb54b079667e784 to your computer and use it in GitHub Desktop.
Python examples
small_words = ('into', 'the', 'a', 'of', 'at', 'in', 'for', 'on')
def book_title(title):
""" Takes a string and returns a title-case string.
All words EXCEPT for small words are made title case
unless the string starts with a preposition, in which
case the word is correctly capitalized.
>>> book_title('DIVE Into python')
'Dive into Python'
>>> book_title('the great gatsby')
'The Great Gatsby'
>>> book_title('the WORKS OF AleXANDer dumas')
'The Works of Alexander Dumas'
"""
lst_of_words = title.split()
new_title = []
for word in lst_of_words:
if word.lower() in small_words:
new_title.append(word.lower())
else:
new_title.append(word.capitalize())
new_title[0] = new_title[0].capitalize()
new_title = ' '.join(new_title)
return new_title
def _test():
import doctest, refactory
return doctest.testmod(refactory)
if __name__ == "__main__":
_test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment