Course 4.1Lesson 1 of 5
split cuts a text into a list of pieces. With no argument it cuts at every run of spaces and drops the extra ones, and with a separator it cuts at that piece only and keeps empty pieces. join glues a list into one text, and the separator comes first. strip removes the spaces at the two ends, replace changes every old piece into a new piece, and startswith, endswith and in answer True or False. A string cannot change, so every method gives a new string, and the result must be kept in a variable.
Try the code and the simulations beside the lesson.
After this lesson you can
split, and glue a list with join,strip, and change pieces with replace,A sentence is a string, but we often want its words. A line of a file is a string, but we often want its fields: the name, the age, the subject. The method split() cuts a string into a list of pieces.
"a b c".split() cuts at every space and gives ["a", "b", "c"]."a,b,c".split(",") cuts at every comma and gives the same list.Predict first. The sentence has spaces at the start and at the end. How many words does split() find? What does "-".join(words) give? Then run it.
split() with no argument throws away the extra spaces, and finds 4 words: the, quick, brown and fox. It treats any amount of white space as one gap."-".join(words) glues the words with a hyphen between each pair: the-quick-brown-fox.split(",") with a separator is stricter. In "a,b,,c", there is nothing between the two commas, so one piece is empty: ["a", "b", "", "c"]. The same happens with split(" ") and two spaces. This is a common surprise. When you split at spaces, use split() with no argument."2026-10-19".split("-") is a good way to cut a date into three pieces.| Method | What it does | Example |
|---|---|---|
split() | cuts a string into a list | "a b".split() gives ["a", "b"] |
"-".join(list) | glues a list into one string | "-".join(["a", "b"]) gives "a-b" |
strip() | removes spaces at the two ends | " hi ".strip() gives "hi" |
replace(old, new) | changes every old piece to the new piece | "cat".replace("c", "h") gives "hat" |
Notice that join is written the other way round from what many learners expect: the separator comes first, and the list goes inside the brackets. Think of it as "put this glue between those pieces".
split() and join() undo each other: split a text at commas, join the pieces with commas, and you have the text again.
Three tools answer True or False: startswith(), endswith() and the word in.
Look at the last part of the program. word.strip() on its own line does nothing that you can see: the line makes a new, stripped string, and throws it away. The variable word still holds the old string with spaces. Only word = word.strip() keeps the result.
Every one of these methods gives you a new string. The old one stays the same, because a string cannot be changed. To keep the result, put it in a variable: tidy = raw.strip().
text.strip() alone changes nothing.split(" ") for words. Two spaces in a row make an empty word. Use split().list.join("-"). The separator comes first: "-".join(list)."-".join([1, 2]) is a TypeError. Change them first: "-".join(str(n) for n in numbers).replace to change only the first match.strip that is too small. It removes spaces only. For other marks, use replace, or strip(".,!") with the marks inside the brackets.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
" aSHA rao ".split() cuts a string into a list. join() glues a list into a string, with the separator first.strip() removes the spaces at the two ends.replace() changes pieces. startswith(), endswith() and in answer True or False.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
"red,green,blue".split(",") give?" ".join(["to", "be"]) give?"a,,b".split(",") give?word.strip() on its own line not change word?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.