Python for Humans · Draft v0.4 · Nov 16, 2026 · Draft position: chapter 1 · Refocus the chapter on strings with Shakespeare searching and counting, move f-strings earlier, and treat input() as review.

Strings, Strings, Strings

Change, search, and build text with string methods and f-strings.

You'll build
Explore a line of Shakespeare, create ridiculous stories, and build a robot that remembers what you tell it.

So far we've worked with two kinds of data: strings and numbers. A string is a sequence of characters (text, really) between single or double quotes. In this chapter, we'll see just how much you can do with them.

Strings

The most likely error you'll make starting out is mixing single and double quotes. As a reminder, read the code below:

'I am a string.'
"And I am also a string."
"Can you combine single and double quotes? It's possible."
'But you "must" be consistent in how you do it.'

I know you are nodding and saying, yes, but I won't make that mistake. I assure you that you will! The good thing about programming is that there are many built-in tools to help us humans avoid such mistakes. Almost every code editor (think of it as Microsoft Word for code, but way more powerful) checks your syntax as you type. We'll set one up later in the course. Just as a typo in a Google Doc shows up with a red underline, a code editor flags mixed-up quotes, along with several other common errors you'll learn about soon.

Details matter when programming, but tools can catch many of the small ones for you. Your job, as the human, is to understand what a program is trying to do.

Okay, so strings! They can do actual things, not just be printed or joined together as we've done so far.

Upper and Lower Case

Want your name in capital letters? Ask Python:

name = "will"
print(name.upper())

That's my name there so feel free to replace it with yours. The dot means, "hey name, please do this" and the word after the dot, upper, is the action. The parentheses at the end mean "do it now." Python calls an action like this a method. Python comes with dozens of methods built into strings.

More likely you want to capitalize just the first letter in a name, right? That's where the built-in capitalize() method comes in.

name = "will"
print(name.capitalize())

Just as there is an upper() method so, too, is there a lower() method to make things lowercase.

greeting = "HI THERE STRANGER!"
print(greeting.lower())

This is the same syntax as we saw before, assigning a string to a variable, greeting here. Then using the lower() method to manipulate it in some way. Try experimenting with some other greetings, also in uppercase, to see how it responds.

In Python 3.14 there are 47 public string methods available for almost every common use case you can think of. You don't need to memorize all of them. Instead, when you think of something you want to do to a string, pause and note that many developers before you have had the same idea and it's probably already built into Python. If it's not, you can write your own code to do it, and later in the course you'll learn how to package that code up as a reusable function.

That's why good documentation is important. Python has prided itself on this and still invests heavily in making sure the official docs are up-to-date and helpful.

Note that these methods are for strings only: if you try them on a number, Python will complain with an error. Change name = "will" in one of the earlier examples to name = 5, without quotes, and you'll see what I mean. Don't worry, numbers come with their own tools, too, and we'll meet them later in the course.

Searching and Counting

Methods aren't just for changing how text looks. They can also answer questions about it. Let's try some on one of the most famous lines in all of Shakespeare. In Romeo and Juliet, Juliet wonders why Romeo's family name should matter at all:

line = "What's in a name? That which we call a rose by any other name would smell as sweet."

print(len(line))
print(line.count("name"))
print(line.count("a"))

Python reports that the line is 83 characters long, that "name" appears 2 times, and that the letter "a" appears 9 times.

Notice that len() looks different from the others. It isn't a string method, so there's no dot. Like print() and input(), it's a built-in function: you put the string inside the parentheses and it tells you the length, the total number of characters. That includes every letter, space, and punctuation mark, even the apostrophe in "What's".

count() is a string method. You put what you're looking for inside the parentheses, and it tells you how many times that text appears.

Now a puzzle. How many times does the letter "w" appear in the line? Count them yourself first, then run this:

line = "What's in a name? That which we call a rose by any other name would smell as sweet."

print(line.count("w"))
print(line.lower().count("w"))

The first line says 4, but the second says 5. What's going on?

To Python, "W" and "w" are completely different characters, so count("w") skips the capital W in "What's". The second line fixes that by making the whole line lowercase first and then counting. You can chain methods together like this, one after another, and Python runs them from left to right.

Find and Replace

count() tells you how many. find() tells you where:

line = "What's in a name? That which we call a rose by any other name would smell as sweet."

print(line.find("rose"))
print(line.find("tulip"))

The word "rose" starts at position 39. Python counts positions starting from 0, not 1, so the very first character, the "W", is at position 0. That feels strange at first, but you'll get used to it.

And "tulip"? Python says -1. That's how find() tells you that it couldn't find something.

Finally, replace() swaps one piece of text for another. Juliet says a rose would smell as sweet by any other name. Let's test that:

line = "What's in a name? That which we call a rose by any other name would smell as sweet."

thing = input("Name something that smells wonderful: ")

print(line.replace("rose", thing))

Try pizza, fresh bread, or rain. Juliet's point still stands!

Notice that replace() doesn't change line itself. It gives you back a new string with the swap made. Add print(line) at the end and you'll see the original, rose and all.

Your turn: Swap in another famous line from Shakespeare, like Hamlet's "To be, or not to be, that is the question." How long is it? How many times does "be" appear? What happens if you replace "be" with something else?

Later in the course, we'll load all of Shakespeare's plays into Python, nearly 200,000 lines of text, and use these same methods to answer questions about all of it. Every word, not just one line.

Build a Mad Lib

Juliet's line was fun to play with, but let's write some ridiculous text of our own.

Have you ever played Mad Libs? It's a word game where you provide a few words without knowing how they'll be used in a story. The results can be pretty ridiculous.

We can make our own version with Python, using the input() function you already met in the Time Machine.

# Try removing the f at the beginning of one of the strings in your
# Mad Lib. What happens when you run it?
# Run the program and enter your answers.

person = input("Name a famous person: ")
animal = input("Name an animal: ")
place = input("Name a place: ")

print(f"One day, {person} met a talking {animal} in {place}.")
print(f"The {animal} announced that {place} was in terrible danger.")
print(f"Luckily, {person} knew exactly what to do: order pizza.")

You might end up with something like this:

One day, Taylor Swift met a talking penguin in Paris.
The penguin announced that Paris was in terrible danger.
Luckily, Taylor Swift knew exactly what to do: order pizza.

Of course, your story will depend on what you typed!

There's something new in our code. Look closely at this line:

print(f"One day, {person} met a talking {animal} in {place}.")

Notice the f before the opening quotation mark and the curly braces {} around our variables.

This is called an f-string, short for formatted string. It's a convenient way to put variable values right inside text. In the Time Machine, we used commas in print() to mix text and variables. F-strings do the same job, but they let you put each value exactly where you want it, right next to punctuation and all.

The f tells Python to evaluate what's inside the curly braces and insert the result. So {animal} becomes whatever animal you entered.

Now try the experiment from the top of the code: remove the f from one of the lines and run it again. Python prints the curly braces and variable names exactly as you typed them, like One day, {person} met a talking {animal} in {place}. Without the f, it's just an ordinary string. Put the f back and everything works again!

You can even use string methods inside the curly braces. Try changing {animal} to {animal.upper()} and see what happens to your story.

Build a Robot

Let's put input, f-strings, and string methods together and build a robot with a personality.

# Try typing your name in all lowercase, then in all capitals.
# What does the robot call you?

print("Hello! I am a very small robot.")
print("I have some very important questions.")

name = input("What's your name? ")
food = input("What's your favorite food? ")

print(f"Nice to meet you, {name.capitalize()}!")
print(f"I also love {food}.")
print("Actually, I cannot eat. This is awkward.")
# Now try adding a third question. Maybe ask for someone's favorite
# animal, color, or place to visit. Save their answer in a new
# variable and make your robot respond.

print("Hello! I am a very small robot.")
print("I have some very important questions.")

name = input("What's your name? ")
food = input("What's your favorite food? ")

print(f"Nice to meet you, {name.capitalize()}!")
print(f"I also love {food}.")
print("Actually, I cannot eat. This is awkward.")

No matter how you type your name, will, WILL, or wILL, the robot greets you as Will. That's capitalize() at work: it makes the first letter uppercase and everything else lowercase.

Notice that Python remembers your name even after asking about your favorite food. That's because we've stored two separate pieces of information in two variables: name and food.

You get to choose variable names yourself. We could have used favorite_food instead of food. Descriptive names make programs easier to understand, especially as they get bigger.

Now one more experiment. In the first tab, change {name.capitalize()} to {naem.capitalize()}, leaving everything else as it is, and click Run.

Python shows a NameError because it doesn't recognize naem. We created a variable called name, but Python treats naem as a completely different name. Fix the spelling and run your program again.

Don't worry about making mistakes like this. Even experienced programmers make them all the time. The important thing is learning to experiment and figure out what went wrong.

Give Your Program a Memory

Our programs can now ask questions, remember answers, and use those answers to create stories.

But what happens when we want to change something the program remembers?

Let's find out.

# Try it yourself: Can you change the program so it asks for a new
# name instead of a new favorite food? What would you need to
# change?
# Try running the program.

print("I'm a robot with a very small memory.")

name = input("What's your name? ")
food = input("What's your favorite food? ")

print(f"Got it! {name} likes {food}.")

food = input("Actually, what food should I remember instead? ")

print(f"Updated! {name} now likes {food}.")

Notice that our robot still remembers your name, but now it uses your new favorite food.

That's because we've assigned a new value to the variable food. Variables don't have to stay the same throughout a program. We can change their values whenever we need to.

Programmers call the information a program keeps track of its state. When we change food, we're changing part of our program's state.

Think about a video game. Your character might start with 100 health points, lose 20 after getting hit, and gain 10 after finding a health potion. The game keeps track of those changes as you play.

Our little robot uses the same basic idea!

There's one important limitation, though: our robot only remembers things while the program is running. Run it again, and it starts fresh.

Later, we'll learn how to save information so programs can remember things even after they've stopped running.

Leave Yourself a Note

Code gives Python instructions, but sometimes we also want to leave a note for the person reading it. That person might be us tomorrow!

# Run the program, then try two experiments:
# - Change the comment to a note of your own. Does the program's
# output change?
# - Put # at the start of the print() line and run again. It still
# asks for your name, but what happens to the greeting?

# Remember the visitor's name for our greeting.
name = input("What's your name? ")

print(f"Welcome to the castle, {name}!")

The first line is a comment. Outside a string, # starts a comment: Python ignores the rest of that line. The note is for people reading the program.

Remove that second # to restore the greeting. Adding # before a line of code is called commenting it out: the line stays in the box, but Python no longer runs it.

Comments can explain what we're trying to accomplish or why we made a choice. You don't need one on every line. A short note where it helps is enough.

Your Turn: Make Something of Your Own

You've learned enough to create something of your own!

Maybe you want to build a robot with a personality. It could be an overly enthusiastic travel agent, a grumpy cooking assistant, or a robot that thinks it's a cat.

Or perhaps you'd rather create a ridiculous story generator, a program that invents silly nicknames, or something completely different.

It's up to you!

Here's a place to get started.

# Replace the code with your own creation.
# Try using print(), input(), variables, f-strings, and string
# methods like upper() or replace() to bring your idea to life.
# Can you also find a way to change a variable's value while your
# program is running?

print("Hello! I'm your new robot.")

Add one comment explaining what part of your program is meant to do. Write it for someone seeing the code for the first time.

Don't worry if you get stuck. Look back at the earlier examples and borrow whatever you need. Try combining pieces from different programs to make something new.

What's Next?

Look how much you can do with strings now. You can change their case, measure them with len(), count and find words inside them, swap one word for another with replace(), and build new text with f-strings. You've used those tools to explore Shakespeare, write ridiculous stories, and build a robot that remembers what you tell it.

Strings are only one kind of data, though. In the introduction, you did a little math with Python and used int() to turn an answer into a number. In the next chapter, numbers get the same attention we've given strings: two different kinds of numbers, some new math tricks, and a program that plans a pizza party down to the last slice.