──────────────────────────────────────

18. Message Lab · Assemble a Team Tag

Turn words into a list and combine them with a separator.

Lesson files · lessons/strings-and-messages/split-and-join

Before you start
- Read text that contains extra spaces.
- Change the letter case of text with upper().

By the end
- Use split() to get the words from a line with uneven spaces.
- Use join() to put an underscore between each pair of words.
- Test a tag made from a single word.

Where you can use it
Make a consistent team tag from names people type. Split the words and join them with underscores so stray spaces give the same tag.

With empty parentheses, split() breaks text into words at whitespace such as spaces or tabs. It keeps the words in order in a list, with every run of spaces serving as one break. For the worked example, that gives ["Red", "Comet"].

In "-".join(words), the hyphen is the separator. Python puts it between the words. Use "_" as the separator for the tag and .lower() on the original line to make the tag lowercase.

Predict
How many hyphens appear between Red and Comet?
- Three.
- One.

Worked example (example.py)

words = "  Red   Comet  ".split()
print("-".join(words))


Example result
Red-Comet

Read a team name and make a lowercase tag with underscores between words.

Starter program (starter.py)

words = "  Red   Comet  ".split()
print("-".join(words))


Hint
Clean the case with lower(), use split(), then join the words with "_".

Case 1. Try the sample
Input
  Red   Comet Club  
Expected output
red_comet_club

Case 2. A single word
Input
Eagle
Expected output
eagle

Stretch
Display both the clean tag and the number of words in the original name.

Reflect
Why is split followed by join useful when a name contains extra spaces?

Check your prediction
One. split() first finds the two words. join() then inserts one hyphen between them.
