thecodezone.
The Code Zone
Short steps, at your pace · Free lessons and practice · Member video walkthroughs
Save your next small win.

Checking your account…

03.05 · Python basics / Watch · 21 min

Read, join and measure Python strings

Text includes the spaces and punctuation you type. Work with a short name first so you can see every character.

Activity 5 of 19 in Python basics

All courses

Python Basics / 03.05

Understand strings

Not started

Create text with matching quotes, join strings, and distinguish a character index from the length of a string.

More options

Your lesson progress

Walkthrough: not startedPractice: not started

Empty: not started. Yellow: in progress. Green: complete. Marking the whole activity complete does not replace your test results. These circles save in this browser for this pilot.

Rob gives Ada the indexes zero, one and two, reads index zero as A, and measures the three-character name.
Rob’s comic strip · Python basics

ORIGINAL WALKTHROUGH · 16:28

Watch with Rob.

Pause whenever you want to try a change. You can return to the lesson at any time.

LEARN WITH THE CODE ZONE

Unlock this video walkthrough

Written lessons and coding practice are free. Video walkthroughs are included with a paid Code Zone membership or an active free trial.

Have a Code Zone website account? Use your learning account’s email to get a six-digit sign-in code, or use your existing website password.

Need a website account? Paid Skool members and active trial members can use the email from their membership questions to request a sign-in code. No invite link or Skool password needed.

Close this window to keep practicing. After signing in, you’ll return to this lesson.

Video walkthroughs are included with an active Code Zone membership. The written lesson and practice are free.

Open video & code editor (opens in a new tab)

Starter source code for VS Code

Copy the source below or download the file to follow along in VS Code.

The download includes a lesson link in each file and a README so you can find your way back.

main.py
main.pypython · example
# Code along with Rob: strings
# Create a text value using matching quotes.
# Join text with + and check spaces.
# Try index 0 and len on a short name.

A string is a text value

A Python string is a sequence of characters. You can use matching single or double quotes around ordinary text. The quotes mark where the text begins and ends; print does not display those delimiter quotes.

If your text includes an apostrophe, double quotes around the whole value can make it easier to read. Triple quotes can hold text across several lines, but they still create strings rather than a special comment type.

Text values use matching quotes
strings.pypython · example
learner_name = "Ada"
message = "It's time to code."
print(learner_name)
print(message)
Open workspace to paste this example (opens in a new tab)

Joining strings does not insert spaces

The + operator joins strings end to end. Python keeps exactly the characters in each part. Include a space inside a string when you want that space in the result.

The example prints Hello, Ada! with one space after the comma. A number is a different kind of value; joining strings with numbers will need a conversion or formatting technique, which later lessons introduce.

Keep the space inside the quoted text
strings.pypython · example
learner_name = "Ada"
greeting = "Hello, " + learner_name + "!"
print(greeting)
Open workspace to paste this example (opens in a new tab)

An index is a position; length is a count

The characters in Ada have indexes 0, 1 and 2. learner_name[0] reads A. len(learner_name) counts all three characters and gives 3. The last index and the total length are different numbers.

Using an index that is outside the string raises IndexError. With Ada, index 3 is already beyond the final character. Start by predicting the first character and length before experimenting in your code-along workspace.

Zero-based position versus total count
strings.pypython · example
learner_name = "Ada"
print(learner_name[0])  # A
print(len(learner_name))  # 3
Open workspace to paste this example (opens in a new tab)

Creating a new string is different from changing one character

Strings are immutable: you cannot assign a different character with learner_name[0] = "E". You can assign a new string to the learner_name variable instead. The variable can refer to a new value even though a string value itself cannot be edited in place.

The video also previews string operations that later activities revisit. For this checkpoint, explain matching quotes, joining text, index zero and length. Then fix one missing space in the practice activity.

Build it one step at a time

Prefer learning by typing? Work through short instructions, run the Python code and check each change before continuing. Your guided project saves separately from the challenge below.

The mini-lesson opens in a new tab. Your own code carries forward; each step has a focused check and an optional hint.

Start mini-lesson(opens in a new tab)

4 steps · Saved work · Instant feedback

YOUR TURN

Give the greeting its missing space

Keep learner_name assigned to "Ada". Build greeting from "Hello, ", learner_name and "!" using +. Include one space after the comma inside the quoted text. Keep both print calls: print the greeting, then print its length with len(greeting). The result should be Hello, Ada! followed by 11.

Expected output
Hello, Ada!
11

Keep the starting inputs. Print these lines in this order, without extra output.

  • learner_name still holds Ada
  • greeting holds Hello, Ada!

Edit Python, then run it. Check challenge checks your output and the listed Python requirements. Your code saves on this browser after a short pause. Sign in and choose Save account draft to keep it across devices. Word wrap is enabled.

Ready to checkRun the checks to see your results here.

Need a nudge?

Spaces are characters too

Python joins the characters you give it; + does not insert a space for you. Put a space after the comma inside the first string.

Count the string, not the code

len(greeting) counts characters in the resulting text, including its space and punctuation. It does not count the quotation marks in your source.

Replace your editor code?

Your current work will be replaced. Choose Cancel to keep it.

Ready to try it?

Practice in your browser and see your results.

Progress details

Have a question?

Ask the Code Zone community.

Ask a question in The Code Zone (opens in a new tab)

Continue learning

ONE STEP FORWARD

Explain the difference between the first character and the total length. Next, fix a greeting whose joined text is missing a space.