Created
October 19, 2015 07:19
-
-
Save accessnash/63c63bb54b079667e784 to your computer and use it in GitHub Desktop.
Python examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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