Split and Join Text
Trace one short label through strip, task-specific lowercasing, whitespace splitting, and joining while preserving every intermediate value.
The measurement program prints labels such as "Mean: 20.5 °C". Human-written
text often arrives with extra space or a mixture of uppercase and lowercase
letters. A program can transform one short label while keeping every step
visible.
source = " Mean Temperature "
The goal in this lesson is the machine-friendly label
"mean-temperature". That lowercase, hyphenated form is a choice made for
this particular task, not a rule for all text.
Remove Surrounding Whitespace
The strip() method removes whitespace from the beginning and end of a string:
It does not remove the meaningful space between the two words. It also does
not change source:
print(source == " Mean Temperature ") # True
Strings cannot be changed character by character. A string method returns new text, so store that returned value when the program needs it later.
Q1. Keep the source text unchanged
What is source after this program runs?
Select one choice, then check.
HintFollow the assignment target
The return value from strip() is assigned to stripped, not to source.
SolutionThe original remains available
source still contains its two leading and two trailing spaces. The new text
without those spaces is stored in stripped.
Lowercase Only When the Task Requires It
For this label, the required output is lowercase. Apply lower() to the
stripped text:
lower() also returns a new string. It does not revise either earlier value:
| Name | Value |
|---|---|
source | " Mean Temperature " |
stripped | "Mean Temperature" |
lowercase | "mean temperature" |
Keeping the names separate makes the transformation easy to inspect. Lowercase is useful here because the output format asks for it. It is not a complete method for comparing every human language or interpreting every piece of text.
Split Text into a Visible Word List
Calling split() without an argument separates a string across ordinary
whitespace and returns a list of words:
Using split() without an argument also handles more than one space, tabs, or
line breaks between words:
The intermediate list matters. It lets the reader see exactly which pieces the program will join. These pieces are whitespace-separated words for this small task; they are not a universal definition of linguistic tokens.
Q2. Inspect the split words
What list does this expression return?
" north\t sensor reading ".split()
Select one choice, then check.
HintIgnore runs of whitespace
The returned list contains the three non-whitespace words.
SolutionReturn three words
The result is ["north", "sensor", "reading"]. The tab and spaces separate
the words but do not become list items.
Join the Parts with One Separator
join() rebuilds text from a sequence of strings. The string before .join
is placed between neighboring items:
For two words, one hyphen is inserted. join does not add a hyphen before the
first word or after the last word.
The complete trace is:
Each line answers a separate question: remove surrounding whitespace, request lowercase text, expose the words as a list, and rebuild them with the chosen separator. A compact chain could perform the same work, but the named intermediate values are easier to inspect and repair.
Q3. Build the report label step by step
Complete the four assignments so the program shows the source text, each intermediate value, and the final label in order.
Editable Python
Ready to run.
HintMake one transformation per line
Call strip() on source, lower() on stripped, split() on lowercase,
and "-".join(...) on words.
SolutionKeep all four returned values
String methods return new text rather than changing the source string.
split() makes whitespace-separated words inspectable as a list, and
join() rebuilds them with a separator chosen for the task. The next lesson
will align each measurement with its position or label.