Split, Strip & Rejoin Words
Tokenize sentences for word problems.
Split, Strip & Rejoin Words is a free Coding Interview Prep lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Coding Interview Prep learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
From Sentence to Words
Word problems usually start by breaking a line into tokens. Python makes this easy with split, and that is your first move. ✂️
split() With No Argument
Calling split() with no argument breaks on any run of whitespace and ignores leading or trailing spaces. It is the safe default.
print(' hi there '.split()) # ['hi', 'there']split() On a Delimiter
Pass a character to split on exactly that delimiter, like commas in CSV-style input. Empty fields are kept as empty strings.
print('a,b,,c'.split(',')) # ['a','b','','c']Watch the Empty Tokens
With a fixed delimiter, two delimiters in a row produce an empty token. Plain split() never does this, which is why it is often safer.
strip() Cleans the Ends
The strip() method removes whitespace from both ends of a string. It is essential when reading lines that carry a trailing newline.
print(' hello\n'.strip()) # 'hello'Strip Specific Characters
Give strip a set of characters and it trims any of them from the ends, not just spaces. Handy for stray punctuation.
print('***word***'.strip('*')) # 'word'join() Glues Tokens Back
The join method does the reverse of split, stitching a list back into one string with a chosen separator between items.
print(' '.join(['hi', 'there'])) # 'hi there'join Needs Strings
Every item passed to join must already be a string. Convert numbers first with map(str, ...) or a comprehension.
nums = [1, 2, 3]
print(' '.join(map(str, nums))) # '1 2 3'join Is the Fast Way to Print
Building one string with join and printing it once beats calling print in a loop. This is a key fast-output habit in contests.
Transform Between Split and Join
The classic combo is split, change each word, then join back. This pattern reverses, filters, or relabels tokens cleanly.
s = 'cat dog fox'
print(' '.join(w.upper() for w in s.split()))Reverse the Word Order
To flip a sentence, split into words, reverse the list, then join. It is a tidy three-step recipe for word-order tasks.
s = 'one two three'
print(' '.join(s.split()[::-1]))Quick Check
One question on splitting and joining.
Recap
You can now tokenize with split, clean ends with strip, and rebuild strings fast with join, the core toolkit for word problems. 🎉
Frequently asked questions
Is the “Split, Strip & Rejoin Words” lesson free?
Yes — the full text of “Split, Strip & Rejoin Words” is free to read here on the web, and the Coding Interview Prep course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Coding Interview Prep course, upgrade to CoddyKit PRO.
What will I learn in “Split, Strip & Rejoin Words”?
Tokenize sentences for word problems. You practise Coding Interview Prep with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Coding Interview Prep?
No prior experience is required. Coding Interview Prep on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Split, Strip & Rejoin Words” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Coding Interview Prep lesson?
Yes. Every Coding Interview Prep lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Characters, ord & chr Tricks
- Count Letters with a Frequency Table
- Palindrome Checks Done Right
- Split, Strip & Rejoin Words