Using Python to Generate Story or Novel Titles - Ryan M. Williams
Python

Using Python to Generate Story or Novel Titles

Writing doesn’t require learning to code. But you can have fun, save time, and improve creativity by learning at least some coding. Even something as simple as creating a word count tracker can help with productivity. Picking up knowledge around HTML and CSS can help writers with websites. Discovering languages like JavaScript or Python unlocks potential for many other options.

I faced this recently with a problem I faced around titles for novels in a series. Don’t worry if you haven’t coded anything before—this is a short example of using Python to generate a list of possible titles for my books. The techniques used can apply to any programming language—or even to doing it entirely by hand.

More...

Branding Series Titles

For the sake of series branding, I wanted to follow the same title format that I used for the first books in the series, in this case Pierce, County, a series set in a fictionalized Washington state with vampires, werewolves, and all sorts of other monsters. Familiar? Think again. In this series the girl turns the tables on the vampires, reacting to them as the pedophiles they are rather than falling in love with them.

The first novel in the series, Dirty Old Vampires, came out a few years back under a pen name. I’ll be releasing an updated version later this year. Then I’ll follow it with Naughty Young Werewolves, and the third book, Pretty Dead Ghouls.

Get the pattern with the titles?

Three words, two adjectives, and a plural noun. The first word is suggestive with a “-y” ending, the second descriptive, and the last refers to a type of monster. I wanted to generate a list of possible titles from combinations of different words.

Creating the Word List

Before starting any coding, I created three lists of possible words, a list for each category. This step requires thinking up words. The lists don’t need to be the same length, but I kept at it until I had lists each with 24 words. I didn’t necessarily plan necessarily to use all of those words, I only wanted to see what combinations turned up.

The lists can be any words you like, with more lists, or fewer. The more lists, the more words in the lists, the greater number of possible combinations. And that is where coding comes in. The number of possible combinations for my lists exceeded 13,000. Although I could generate them by hand, it’d take a while to even list all of them, much less consider them. It's faster to use the code to generate the list.

After I review the code used for generating word combinations, I'll talk about how I went about narrowing that list down. Fortunately, the process didn't take too long.


Random Titles

You can skip the coding part and use word lists like this to generate title ideas. Pick a word from the first column, one from the second, and one from the third. If you’re using Excel and want to create random titles, use a formula like:

=INDEX(Table1[First],RANDBETWEEN(1,ROWS(Table1[First])))

The formula refers to the column of a table containing your words. You'd use the same formula in adjacent cells, except change the reference to the other columns of your table. Press F9 to refresh the sheet and generate a new random title.


Example Word List

Here's a portion of the word list I used in generating titles. I'll use these lists in the example code.

First

Second

Third

Crazy

Innocent

Reapers

Curvy

Bold

Gargoyles

Fleshy

Live

Sirens

Flirty

Cute

Zombies

Kinky

Brunette

Ghosts

Lusty

Wild

Demons

Steamy

Ancient

Angels

Sultry

Blonde

Mummies

Here's another way to try title combinations: in a table like this one, sort by the different columns to quickly see different combinations. 


Introducing Python With TitleGen.py

Here's the code I used for this simple title generator. I'm not a Python expert. I like experimenting and trying things out, but I'm still learning.

titlegen.py Python

The first lines with the hash mark # are comments. 

"import itertools" adds a module with code to help with combinations.

An array is a list. These are the word lists that I'm using for this example. You can see that they are inside single quotes and the comma goes outside the quote. If you want a possessive word use double-quotes instead (just be consistent).

The titles array is empty at the start.

def gen_titles creates a function, a piece of code, that actually generates the titles, adding each to the previous empty titles array.

Next is another function, show_titles, that sends the titles to the Python shell.

Finally, the code runs the two functions created, generating and showing the titles. 

Python Generated Titles

The example list of eight words in each group generated 512 possible titles as it iterated through the combinations. The text output can be copied and pasted into other programs.

Python output

This isn't the only way this could work. For example, if I only wanted to look at possible titles for zombies, I could run the program with only zombies in the third list and generate a list of titles that used every combination of the other words. That's similar to what I actually did to narrow down a list of over 13,000 items.

Reducing the Title List

Even with a long list of titles, some titles will catch your attention, e.g., Flirty Live Gargoyles. With this series, I don't want to use any of those words again, so I delete them from the arrays and run the program again. In that way, I can quickly reduce the number of titles by removing words used that I don't want to repeat and running the program once more. If you're using the random title method, the same technique works. Take out the words used and run it again. You can repeat that as many times as you like until you run out of possible combinations that interest you.

Adapting TitleGen.py

Nothing requires the program to remain structured the way I have created it. Don't want a three-word title? Leave an array empty or remove it entirely. If you remove the array, you'll need to update the for loop in the gen_titles function. The key part is the itertools.product(first, second, third). If you removed an array, removed the reference from the product function, e.g. itertools.product(first, second). Likewise, if you want to add an array, create it like the others in the program and add a reference to the product function, e.g., itertools.product(first, second, third, fourth).

Your arrays also don't need to be single words. You could have an array with prepositions, prepositional phrases, articles. Or one with verbs. Maybe you want words ending in "-ing." You can create word lists from whatever you want and an entry in the array can include multiple words as a single entry, e.g., "of the" might be an entry. Customize it however you like.

Resources for Learning Python

Get started learning Python at your local library. There's a pretty good chance that they will have books to teach Python. My local library offers Lynda.com ($30/month value) which has courses on learning Python. You can also check out free resources online such as W3Schools and the Python.org website. Whatever resources you use, you'll want to visit the Python.org website to get started and install Python on your system.

It isn't necessary for a writer to learn Python, but its usefulness isn't limited to creating a quick title-generating program. There are many other different ways that Python can be used to help you in your creative efforts. Or any other programming language that you choose to learn. Writing is all about discovery. Learning Python is also about discovery. Go on an adventure. And while you're at it, go ahead and sign up for Readinary, my weekly email below.

About the Author Ryan

Ryan M. Williams lives a double life as a full-time career librarian and a multi-genre writer with over twenty books. He writes across a range of genres including science fiction, fantasy, paranormal, mystery, horror, and romance. He earned a Master of Arts degree in writing popular fiction from Seton Hill University and a Master of Library and Information Science from San Jose University. His short fiction has appeared in anthologies from Pocket Books, WMG Publish, and in On Spec Magazine.

follow me on:

Leave a Comment: