FoundationsLesson 4 of 1916 min
Working with Text
Build, format, slice, and clean up strings — the type you will handle more than any other.
By the end you will be able to
- Combine strings with concatenation and f-strings
- Index and slice to extract parts of a string
- Use common string methods like upper(), strip(), replace(), and split()
A string is text. Single and double quotes work identically — pick whichever avoids escaping the quotes inside your text. Triple quotes span multiple lines.
Hello Hello
It's fine
Line one
Line two
— expected output; press Run to execute it yourselff-strings: the way to build text
Putting an f before the opening quote lets you drop expressions straight into the text inside {}. This is clearer and less error-prone than gluing pieces together with +.
Hi Ada, you have 20 lessons.
Hi Ada, you have 20 lessons.
Half done means 10.0 lessons.
ADA has 3 letters.
— expected output; press Run to execute it yourselfIndexing and slicing
Characters are numbered from 0. Negative numbers count back from the end, so -1 is the last character.
A slice text[start:stop] takes everything from start up to but not including stop.
P
n
Py
thon
Py
nohtyP
6
— expected output; press Run to execute it yourselfMethods you will use constantly
A method is a function attached to a value, called with a dot: value.method().
CodeNest
CODENEST
Hello
['a', 'b', 'c']
2026-08-09
PYthon
True
True
— expected output; press Run to execute it yourselfExercise
Given raw = " ada LOVELACE ", produce clean equal to "Ada Lovelace" — trimmed, with each word capitalised. Then print a greeting: Hello, Ada Lovelace!
Press Run to execute this code.
Exercise
Write a function initials(full_name) that returns the uppercase initials of a name. initials("ada lovelace") should return "AL".
Press Run to execute this code.