Nex2School

Parent guide · 4 min read

Python strings for kids: changing the text a program holds

Almost every beginner program works with text: a name, a message, a guess typed at the keyboard. In Python, text is called a string. Most early errors come from forgetting that a string is not a number, even when it looks like one. This guide covers how to join strings, put values inside them, check whether a word is in one, and turn typed input into numbers.

Try this tonight

4 things you can do at home

  1. Print three strings and join them

    Run first = "Mia" and last = "Park", then print(first + " " + last). Notice the " " in the middle: without it the names run together. Predict the output before pressing run.

  2. Try an f-string

    Run name = "Mia" and age = 9, then print(f"{name} is {age} years old"). The f before the quote lets the curly brackets hold a value. Change age and run it again.

  3. Ask whether a word is inside

    Try word = "butterfly" and print("fly" in word), then print("cat" in word). The first is True and the second is False. This is the usual way to search inside text.

  4. Make the input() error on purpose

    Run age = input("Age? ") and print(age + 1), then type 9. Python complains that a string and an int cannot be added. Now fix it with age = int(input("Age? ")). Seeing the error first makes the fix stick.

  1. A string is a row of characters

    Python keeps a string as characters in order, a little like a list of letters. len("butterfly") is 9, and "butterfly"[0] is "b", because counting starts at 0. A child who has met lists will find strings familiar.

    One difference matters: a string cannot be changed in place. To change a letter you build a new string, for example "b" + word[1:], and give it a name.

  2. Worked example: why "5" + "3" is not 8

    Quotes make a string, so "5" is a character that looks like a digit, not the number five. "5" + "3" joins the two pieces of text and gives "53". Without quotes, 5 + 3 is 8. A string times a number repeats it: "ha" * 3 is "hahaha".

    This is why input() trips up beginners. What the child types arrives as text, so "9" + 1 fails. int("9") turns the text into the number 9, and then 9 + 1 is 10. Going the other way, str(10) gives the text "10", which is needed to join a number into a sentence with +.

  3. Worked example: tracing a small program

    Take name = input("Name? ") and typing Leo. Then greeting = "Hi " + name gives "Hi Leo". len(name) is 3. "e" in name is True. name.upper() gives "LEO", and name still holds "Leo", because methods like upper() hand back a new string.

    Writing each value down on paper, line by line, then running the code to compare, is the best check that a child understands rather than copies. The same trace works for replace: "banana".replace("a", "o") gives "bonono".

  4. The slips beginners make most

    Mixing up quote types, such as starting with a double quote and ending with a single one, causes a SyntaxError. A missing f before an f-string prints the curly brackets as they are. Case matters: "Leo" in "leo" is False.

    Joining text and a number with + gives a TypeError, fixed with str() or, more simply, an f-string. Reading the last line of the error message aloud usually names the cause.

Signs it’s time for outside help

When home help has done what it can

  • Gets a TypeError when adding a number to text and cannot say why.
  • Forgets that input() gives text, even when a number was typed.
  • Leaves out the f in an f-string and sees curly brackets in the output.
  • Mixes up quotes, brackets and the = sign.
  • Copies string code that works but cannot predict its output before running it.

Questions parents ask

FAQ

What is a string in Python?

A string is a piece of text in quotes, such as "hello". Python treats it as characters in order, and you can join strings, repeat them, search inside them and print them.

How do I turn input() into a number?

input() always returns a string. Wrap it in int() for whole numbers or float() for decimals, for example age = int(input("Age? ")).

How do I check whether a word is in a string?

Use the in keyword: "fly" in "butterfly" is True. It is case-sensitive, so lower() on both sides helps when capitals could differ.

What is an f-string?

An f-string starts with f before the opening quote, and curly brackets inside hold values or expressions, such as f"{name} is {age}". It is the simplest way to mix text and values.

Not sure which is right for your child?

Tell us their age and what they have tried so far, and we will suggest where to start before you book anything.

Keep reading