Skip to content

Lesson 03

Python Strings

Strings are the chief form of data the digital humanist works with. Learn the built-in functions that operate on them.

As noted in Lesson 02, strings are objects delineated by double or single quotation marks. They are the key way we handle text in Python, which means strings are fundamental for the digital humanist — they are the chief form of data we work with.

This lesson covers how to interact with strings as pieces of data. The way we interact with data in Python is by calling functions. The basic string functions ship with Python itself, so you don’t need to install anything third-party. Later in this series we’ll do more advanced work with strings using libraries like re (regex), but for now we’ll stick to the built-ins.

A quick word on functions

A function is a block of code stored somewhere — either in your own script or inside Python itself. We call a function by writing its name followed by a pair of parentheses. Often we pass arguments inside those parentheses: the pieces of data the function will operate on, separated by commas.

Many string functions are methods, meaning we call them on a string by writing the string, a period, and then the method name. For example, "hello".upper() calls the upper method on the string "hello".

split — break a string into pieces

The split method breaks a string apart wherever it finds a given substring and returns a list of the pieces. Here we tell Python to split on a comma:

a_string = "Hello, Bye"
new_string = a_string.split(",")
print(new_string)

Output:

['Hello', ' Bye']

Notice three things. First, strings are immutable, so split does not change a_string — it returns a new value, which we capture in new_string. Second, the result is a list of strings, not a single string. Third, the space after the comma was preserved, which is why we get ' Bye' rather than 'Bye'. Splitting on ", " (comma plus space) would clean that up.

replace — swap one substring for another

The replace method takes two arguments: the substring to find, and the substring to put in its place. Both are required. Continuing with the same input:

a_string = "Hello, Bye"
new_string2 = a_string.replace("Hello", "Tacos")
print(new_string2)

Output:

Tacos, Bye

Again, the original a_string is untouched. replace hands back a brand-new string with the substitution applied, and we store it in new_string2.

Other useful string methods

The video walks through several other built-in methods. Each of these is called the same way — your_string.method(arguments):

  • find — return the index where a substring first appears (or -1 if it isn’t present): "hello world".find("world") returns 6.
  • join — glue a list of strings together using the calling string as the separator: ", ".join(["a", "b", "c"]) returns "a, b, c".
  • upper — return the string in uppercase: "hello".upper() returns "HELLO".
  • lower — return the string in lowercase: "HELLO".lower() returns "hello".
  • format — fill placeholders in a string with values: "Hello, {}".format("world") returns "Hello, world". (Modern Python more often uses f-strings: f"Hello, {name}".)
  • count — count how many times a substring appears: "banana".count("a") returns 3.
  • in — not a method, but a Python keyword for membership: "world" in "hello world" returns True. This is the modern equivalent of “contains.”

Try each of these on a string of your own. Once you’re comfortable with how they behave, move on to Lesson 04.

Running the code

Save any snippet from this lesson to a file — say try.py — and run it from your project folder:

uv run try.py

uv run uses the project’s Python and dependencies automatically; no virtualenv to activate. If you haven’t set the project up yet, Lesson 01 walks through it.