Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def nice_title_case(str_in):
- """Converts a string to Title Case, with conventional exceptions which
- need not be capitalized if they are not the first word in the title.
- """
- exceptions_to_title_casing = (
- "a", "an", "and", "at",
- "but", "by", "for", "in",
- "nor", "of", "on", "or",
- "so", "the", "to", "up", "yet"
- )
- str_inter = str_in.title()
- title_word_list = str_inter.split()
- new_title_list = []
- title_length = len(title_word_list)
- if (title_length > 1):
- new_title_list.append(title_word_list[0])
- for elem_cntr in range(1, title_length):
- if title_word_list[elem_cntr].lower() in exceptions_to_title_casing:
- new_title_list.append(title_word_list[elem_cntr].lower())
- else:
- new_title_list.append(title_word_list[elem_cntr])
- final_output = ' '.join(new_title_list)
- else:
- final_output = str_inter
- return final_output
- if __name__ == "__main__":
- """Just a quick test of the `nice_title_case` function
- """
- my_old_title = "the first title is but a temporary title of a better or worse title"
- my_new_title = nice_title_case(my_old_title)
- print("Orig. title: '{}'".format(my_old_title))
- print(" New title: '{}'".format(my_new_title))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement