Module 03 · Lesson 03
Strings & Text Representation
Work with text as a sequence of characters: write strings in single, double or triple quotes, index and slice them, join and repeat them, and understand why they cannot be changed in place.
- About 8 minutes
- Beginner friendly
- 12-question quiz at the end
What you will learn
- Index and slice text
- Join and repeat
- Loop over letters
- Make a secret code
Text is a row of characters
A string is text in quotes. Python sees it as characters in a row, so lots of list tricks work on it too.
Quotes
"hi"and'hi'are the same. Pick the one that does not clash with the text.Index and slice
word[0]is the first character,word[-1]the last andword[1:3]a piece.Fixed
You cannot change one letter in place. You build a new string instead.
Join and repeat
+joins text,*repeats it andlen()counts its characters.
Text runs the world
Most of what you read on a screen is a string somewhere in a program.
- Messages
len()checks a message is short enough before it is sent. - UsernamesSlices take the first letters of a name to suggest a username.
- Search
inchecks whether the words you typed appear in a title. - SubtitlesOne string type handles every language and alphabet in the world.
Cutting out a piece
A slice copies part of the text. Tap each part to see its job.
Part 1 of 5
text:Six characters at indexes 0 to 5: P is 0, Y is 1, T is 2 and so on.
Prints YTH
Take text apart
Pick an example, change it, then press Run. Say what you expect to see before you press.
Example 1 of 6Index it
Every character has a position, counting from 0.
Your turn: Print the middle letter using len(word) // 2 as the index.
word = "rainbow" print(word[0]) print(word[-1]) print(len(word), "letters")
Press Run to see what it prints.Watch a word turn into a secret code
Press Step to run one line at a time. Slices and joins turn planet into a playground secret language.
Ready. Press Step to run the first line.
- word = "planet"
- first = word[0]
- rest = word[1:]
- secret = rest + first + "ay"
- print(secret)
- print(len(word), len(secret))
Output so far
Nothing printed yet.
What Python does
Python reads the program from the top. Nothing has run yet.
Names
None yet.
Quick guesses
Guess first, then read why. A wrong guess is where the learning happens. Nothing here is scored.
Question 1 of 6
1.What does this print?
print("abc"[-1])0 of 6 answered
Common string misconceptions
Each card is something beginners often believe. Tap it to see what Python really does.
Misconception 1 of 4
Notes
Everything above, in words. Open it when you want the full explanation.
Read the notesNotesOpenClose
Syntax
The general shape of the statements in this lesson.
s = "text" s = 'text' s = """several
lines"""
len(s) s[0] s[-1] s[1:4] s[::-1]
s + "more" "-" * 10 "ex" in s
s == t s < t # exact, dictionary order
"B" + s[1:] # build a new string
ord("A") chr(65)Press Run to see what it prints.Worked examples
Read the problem, run the code, then read why it works.
Example 1 of 2
Initials and a username
From a first and last name, print the initials and build a username from the first three letters of each name in lowercase.
first = "Grace" last = "Hopper" initials = first[0] + "." + last[0] + "." username = (first[:3] + last[:3]).lower() print(initials) print(username)
Press Run to see what it prints.Predict first
How it works(open after your guess)
Common pitfalls
Mistakes people make with this topic, and what to write instead.
Pitfall 1 of 3
Trying to change one character
Spot the bug
Can you see what is wrong? Run it to watch it go wrong, then open the fix.
word = "cat" word[0] = "b"
Press Run to see what it prints.Show what goes wrong and the fixWhat goes wrong and the fix
In the real world
Where this shows up in software people use every day.
- Retail
Masking a card number on a receipt
Card receipts never show the whole card number. The till keeps the last four digits and replaces the rest with stars, so customers can recognise their card without exposing it.
Why Python fits
Repeating a star for every hidden digit and slicing the last four builds the masked text without touching the original. Slicing in steps of four then groups it the way people expect to read a card number.
Pythoncard = "4929123456781234" masked = "*" * (len(card) - 4) + card[-4:] grouped = "" for i in range(0, len(masked), 4): grouped += masked[i:i + 4] + " " print(grouped)OutputPress Run to see what it prints.
Key takeaways
Tick each one you could explain to a friend.
0 of 5
Common questions
Guess the answer first, then open it.
Should I use single or double quotes?
How do I get the last three characters?
Why does len("hi there") give 8?
Is an empty string the same as a space?
Your notes
Sign in to keep private notes alongside this lesson.
Where to go next
The quiz and the Lab live on their own pages, so this lesson stays about learning.
Finished this lesson?
Create an ID or sign in to save your progress across devices.
Check what you learned
12 questions, with an explanation after every answer. A verified attempt counts towards opening the module Lab.
Keep going in the Lab
7 tasks on this lesson, from writing code to tracing it by hand. Practice only, no XP.
Spaced review
Questions you miss in a quiz come back on a spaced schedule, so what you learned here sticks.
This lesson at a glance
The 7 lines of code this lesson uses, each with a short note. Keep it open while you do the quiz and the Lab.
What else you can search for and explore
Curious to go further? Each card opens a web search in a new tab.
References
Look things up here whenever you need the exact details.