Skip to content
Stories

Meet the Method: Python for Poetry

Written by Sara Morrell and Zhen Guo

This NULab blog series serves to showcase some of the DITI’s publicly available learning resources. This installment focuses on Python.

Lots of things can be scary on Halloween, but computer programming doesn’t have to be one of them! This Halloween, we will use the computer programming language Python to write a spooky poem and unmask some introductory fundamentals of coding. 

Integrating computational attributes into the writing process, such adding randomness, can give authors another literary dimension to explore by making their work dynamic and changeable instead of fixed. In response to the increasing presence of AI, there has been a lot of discussion about what literary co-creation between humans and machines can or should look like. However, computational poetry emerged well before modern generative AI. The first well-known computational poem was published in either 1959 or 1677, depending on whether you think ‘computational’ necessitates the use of computers. We will follow the example of “The House of Dust” written by Alison Knowles and James Tenney in 1967 (code reimplemented in Python by Nick Montfort) to illustrate some beginner-friendly Python code you can use to integrate computational elements into your own writing. 

In this blog post, we will discuss how to manipulate text in Python and add an element of randomness to the text our program prints out. To run (and edit!) the code we describe in this post, go to this Google Colab notebook. To save your changes, make a copy of the notebook and save it to your Google Drive. Alternatively, you can download the notebook (file extension IPYNB) and run it on your computer

To construct our poem, we will use strings (text), variables, lists, and functions. These fundamentals will give us the tools we need to write a poem that randomly changes each time we run its code.  Text in Python is called strings, and there are several ways we can store, access, and manipulate these strings. Strings can be written with single or double quotes. A string can include letters, line breaks, whitespace characters (like tab characters and spaces), and numbers. For example, 'cats', "bats", and 'friendly ghosts' are all strings. We can put strings together, or concatenate them, using a + sign.  For example, 'spooky'+' '+'night' becomes 'spooky night'.

Variables in Python point to specific information. There are many kinds of information and data types that a variable can point to, including numerical and text data. We create (or initialize) a variable by assigning it a value using the = sign.  For example, if we want the house in our poem to be inhabited by bats, we could set the variable inhabitants to be bats:

inhabitants = 'bats'

To assign multiple values to a variable we can use a list. Lists in Python can store anywhere from zero to millions of values. For example, if we want the house in our poem to be inhabited by bats, cats, or friendly ghosts we could add all of these to a list. Just like creating a variable, we use the = sign to create a list:

inhabitants= ['cats', 'bats', 'friendly ghosts']

Lists in Python use square brackets[] and separate each value with a comma. We can also create lists of potential materials, locations, and light sources for our house:

material = ['candy corn', 'crumbling stone', 'spider webs', 'pumpkins']

location = ['in a haunted forest', 'at the end of a forsaken path', 'by an abandoned lake']

light_source = ['one thousand candles', 'lightning', 'moonlight']

To do things with our text, like print it out or select random parts of it, we can use functions. In computer programming, a function is a collection of code which can be applied and reapplied to complete repetitive or similar tasks. In general, functions can be thought of as a kind of recipe where you plug in a set of ingredients and the function follows the recipe to get the desired output. Some functions are built into Python, like print(), but others need to be imported from modules. Modules are files containing many functions to choose from, like cookbooks that have a collection of recipes. To randomly choose items from our lists we will import the choice function from the random module:

from random import choice

When we run the choice function on one of our lists, it will return a random item from our list. For example, if we run choice(light_source)we will randomly get ‘one thousand candles’, ‘lightning’ or ‘moonlight’. While we will be using functions that are already defined, you can also write your own functions. For more information check out our handout on Python functions.

Now, we have all the tools we need to write a computation poem! In the below example, we first import the choice function from the random module.

Caption: Python code to import the choice function from the random module using the code from random import choice.

Then, we create four lists holding the strings we will randomly draw from to create our poem. 

Caption: Python code to create four lists and assign them to the variables material, location, light_source, and inhabitants.

To put our poem together, we will print a predefined string and then a string that has been randomly selected using choice() from our lists. We will use four print functions to print out the four lines of our poem.  Since we are using the choice function, our poem changes each time its code is run.

Caption: Python code to randomly select items from lists and print out our poem.

Click play on the video below to see the poem in action!

Caption: A video of running Python code to randomly select items and print out a poem. Since this code uses the choice() function to randomly select items from lists, the poem changes each time the code is run.

To edit this poem, or write your own, make a copy of the Colab notebook and revise the code. Scroll down to the “Poetry Example” section to see how this poem is constructed. While coding can be tricky, writing your own computational poem is a treat! To learn more about Python, you can check out our Python Poetry and general Python modules on GitHub.  

More Stories

Decorative NULab logo.

Encoding the Letters of Ignatius Sancho with the Mapping Black London Team 

08.14.2025
word cloud of frequently appearing words in the corpus (aka ‘the canon’) of AI ethics course texts

Analysing AI Ethics… Using AI!

07.01.2025
Fall Celebration attendees watch a lightning talk presented by DSG-affiliated scholars

Fall 2025 Digital Scholarship Celebration

11.04.25
Blog