Working with Strings¶
Summary¶
Text in Python is called a string. This chapter introduces the string type,
single vs double quotes, concatenation (joining strings), repetition, escape
characters like \n, measuring length with len(), raw strings, and
string immutability. As a practical project you'll use turtle.write() to
draw labels and text directly onto your turtle canvas.
Concepts Covered¶
This chapter covers the following 9 concepts from the learning graph:
- String Type
- Single vs Double Quotes
- String Concatenation
- String Repetition
- Escape Characters
- String Length with len()
- String Immutability
- Raw Strings
- Writing Text with turtle.write()
Prerequisites¶
This chapter builds on concepts from:
By the end of this lesson you'll be able to:
- Create strings using either single or double quotes
- Join strings with
+(concatenation) and repeat them with* - Use escape characters like
\nand\tto control text layout - Draw labeled text directly onto the turtle canvas with
turtle.write()
Numbers are not the only data Python works with. Text — names, sentences, emoji, file paths, URLs — is just as important. Python calls text a string, and this chapter gives you the tools to create, combine, and display strings in your programs.
Welcome to Chapter 8!
Strings open up a whole new side of Python! Once you know how to work with text, your turtle programs can draw labels, scores, and messages right onto the canvas. Let's code it together!
The String Type¶
A string is any sequence of characters — letters, digits, spaces, punctuation, emoji — wrapped in quotation marks.
Python stores strings with the type name str.
1 2 3 | |
Notice that "42" (with quotes) is a string, not the number 42.
If you tried to add "42" + 1 Python would give you an error — strings and numbers are different types.
Single vs Double Quotes¶
Python accepts both single quotes '...' and double quotes "..." for strings.
They work identically — choose whichever you prefer, just be consistent.
The main reason to know both is when your string contains an apostrophe or quotation marks:
1 2 | |
If you accidentally use the same quote type inside and outside the string, Python gets confused and gives you a SyntaxError.
String Concatenation¶
Concatenation means joining two or more strings end-to-end.
Python uses the + operator for concatenation:
1 2 3 4 | |
You can concatenate as many strings as you like in a single expression.
Notice that Python does not add a space automatically — you have to include " " yourself if you want one.
The program below builds a greeting message from two variables.
What Do You Think Will Happen?
Look at the last print() call before you click Run.
name is "Alex" and greeting is "Hello, ".
What exactly will the program print on that last line?
Make your guess — then find out!
Try It Now¶
Edit the code below and click Run to see the result.
Were you right? The first print() outputs Hello, Alex!. Notice how "-" * 20 creates a divider line of exactly twenty dashes — much faster than typing them one by one!
String Repetition¶
Just as + joins strings, * repeats a string a given number of times:
1 2 | |
String repetition is handy for creating visual separators, borders, and patterns.
The number on the right must be an integer — "ha" * 2.5 gives an error.
Escape Characters¶
Some characters can't be typed directly inside a string.
Python provides escape characters — special two-character sequences that start with a backslash \:
| Escape | Meaning | Example output |
|---|---|---|
\n |
New line | Moves to the next line |
\t |
Tab | Adds a tab-sized space |
\\ |
Literal backslash | Prints \ |
\' |
Literal single quote | Prints ' inside a single-quoted string |
\" |
Literal double quote | Prints " inside a double-quoted string |
1 2 3 | |
The \n (newline) is one of the most-used escape characters — it lets one print() call produce multiple lines of output.
Scratch Bridge
In Scratch, the Say block displays one message at a time in a speech bubble. Python's print() is similar, but much more flexible — you can print multiple lines, add tabs, join variables and text, and display numbers all in one call!
String Length with len()¶
The len() function counts how many characters are in a string — including spaces and punctuation:
1 2 3 4 5 | |
len() is one of Python's built-in functions — you don't need to import anything, it just works.
You'll use len() constantly: checking if a password is long enough, measuring user input, or controlling how many characters to display.
String Immutability¶
Once a string is created, you cannot change individual characters inside it. This property is called immutability:
1 2 | |
This surprises beginners who expect strings to work like a list. To "change" a string, you create a new string from the old one:
1 2 3 | |
Immutability is not a bug — it makes strings faster and safer to use.
You will learn string slicing (the [1:] part above) in a later chapter.
Raw Strings¶
Sometimes you want Python to ignore escape characters entirely.
For example, if you are writing a Windows file path like C:\new_folder\text.txt, the \n and \t would be interpreted as a newline and a tab!
A raw string is created by putting r before the opening quote.
Inside a raw string, backslashes are treated as literal characters:
1 2 3 4 5 | |
Raw strings are most commonly used for file paths and patterns (which you'll learn about in Chapter 29).
Watch Out!
A raw string cannot end with an odd number of backslashes. r"path\" gives a SyntaxError because the last \" looks like an escaped quote mark. If you need to end a string with a backslash, just use a regular string and write "\\" (two backslashes = one literal backslash).
Writing Text with turtle.write()¶
The turtle.write() function draws a text label directly onto the canvas at the turtle's current position.
This lets your drawings include titles, captions, scores, or any other text.
The basic form is:
1 | |
The font argument is a tuple of three values: the font name, the size in points, and the style ("normal", "bold", or "italic").
The program below draws a polygon and then labels it.
Your Turn — Add the Label
The program below draws a pentagon but doesn't label it yet.
Add one line after the loop to call t.write() and display the text "Pentagon" on the canvas.
Hint: move t.penup() and position the turtle first with t.goto(-40, -120), then call t.write().
Once the label appears, try changing the font size, style, or the text itself!
Experiments¶
Try these changes to the programs above. For each one, predict what will happen first, then run it to check!
- In the first lab, change
name = "Alex"to your own name. You'll know it worked when the greeting prints your name. - Change
"Ha" * 4to"Ha" * 10. You'll know it worked when you see ten "Ha"s in a row. - In the turtle lab, add a
print(len("Pentagon"))line. You'll know it worked when you see8printed below the canvas. - Change the label text to
"My " + str(sides) + "-sided shape". You'll know it worked when the label reads "My 5-sided shape". - Try changing
sides = 5tosides = 8and the label to"Octagon". You'll know it worked when you see an eight-sided shape with the correct label.
Great Work!
You've mastered Python's string type! You can join strings with +, repeat them with *, add newlines and tabs with escape characters, measure their length with len(), and even draw text labels right onto the turtle canvas. Strings are everywhere in programming — and now you know how to use them. Let's keep coding it together!