TwiddleLab / Code / Build with Python
Strings Are Beads
What you will learn
- A string is a row of characters, like beads on a thread. name[0] is the first bead.
- A negative index counts from the end: name[-1] is the last bead.
- A slice name[start:stop] is a frame: start is inside, stop is not. name[0:5] takes 5 beads.
- Slices joined with + make a new string.
How the lesson goes
Your username is pixel_fox_2009. The game shows only the first 5 letters.
Drag the frame's edges so it holds exactly those 5 letters.
Which bead is name[-1]?
A minus sign inside the brackets. Tap the bead you think it gives.
Run it, one line at a time.
Watch which bead lights or where the frame goes before the output prints.
Strings, indexes and slices.
A string is text in quotes: a row of characters. len(name) counts them: 14 here.
An index picks one character: name[0] is the first. A negative index counts from the end: name[-1] is the last, name[-2] the one before.
A slice name[start:stop] cuts a piece: start is in, stop is out. Leave one out and it runs to the edge: name[:5] is pixel, name[-4:] is 2009.
Cut and join your own gamer tag.
Each piece is a slice of name. Join two or three into a tag of 4 to 9 characters.
Slice sniper: frame each target, then Fire.
Five targets. Fewest misses wins three stars.
A slice has a secret third number.
It's the step. Try each one on your tag.
Next lesson: Text Tricks · Part of the course: Build with Python
Lessons are interactive and need JavaScript. Turn it on to play them.