SHARE
TWEET

Untitled

a guest Mar 23rd, 2019 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def nice_title_case(str_in):
  2.     """Converts a string to Title Case, with conventional exceptions which
  3.     need not be capitalized if they are not the first word in the title.
  4.     """
  5.     exceptions_to_title_casing = (
  6.         "a", "an", "and", "at",
  7.         "but", "by", "for", "in",
  8.         "nor", "of", "on", "or",
  9.         "so", "the", "to", "up", "yet"
  10.     )
  11.     str_inter = str_in.title()
  12.     title_word_list = str_inter.split()
  13.     new_title_list = []
  14.     title_length = len(title_word_list)
  15.     if (title_length > 1):
  16.         new_title_list.append(title_word_list[0])
  17.         for elem_cntr in range(1, title_length):
  18.             if title_word_list[elem_cntr].lower() in exceptions_to_title_casing:
  19.                 new_title_list.append(title_word_list[elem_cntr].lower())
  20.             else:
  21.                 new_title_list.append(title_word_list[elem_cntr])
  22.         final_output = ' '.join(new_title_list)
  23.     else:
  24.         final_output = str_inter    
  25.     return final_output
  26.  
  27. if __name__ == "__main__":
  28.     """Just a quick test of the `nice_title_case` function
  29.     """
  30.     my_old_title = "the first title is but a temporary title of a better or worse title"
  31.     my_new_title = nice_title_case(my_old_title)
  32.     print("Orig. title: '{}'".format(my_old_title))
  33.     print("  New title: '{}'".format(my_new_title))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top