1
Hatchling0 / 300 XP
0day streak 0stars
Your Python practice
Mock tests Readiness
More
Guest mode · saved on this device.

Building strings

Strings cannot be changed in place, so you build a new one. There are two reliable ways:

out = ""
for ch in word:
    out = out + ch * 2      # grow a string piece by piece
parts = []
for ch in word:
    parts.append(ch.upper())
result = "".join(parts)     # collect, then join once

Reach for these tools:

  • text.split() breaks on any run of spaces and drops empty pieces. text.split(" ") does not: two spaces in a row give an empty piece.
  • f"{n:02d}" pads a whole number with zeros to 2 digits: 7 becomes "07".
  • "-".join(list_of_strings) glues pieces with a separator.

When a rule says "keep this character, change that one", loop over the characters and decide for each one.

Watch out grow one piece per pass.

See it run

word = "tree"
out = ""
for ch in word:
    out = out + ch + "-"

A loop that builds a new string, one character per pass.

Practice building strings

Exercise 1

A club writes members' names on name tags using only their initials. Given text, a sentence of words made of letters, return the uppercase first letters of its words joined into one string.

Rules:

  • Words are separated by one or more spaces. text may start or end with spaces.
  • Text with no words gives the empty string "".

Constraints: 0 ≤ len(text) ≤ 1000, only letters and spaces.

Example: solution(" grace brewster hopper ") returns "GBH".

Exercise 2

A receipt must hide a card number. Given text, replace every digit with "*" except the last 4 digits, which stay visible. Characters that are not digits (spaces, dashes, letters) stay exactly where they are.

Return the new string. If text has 4 or fewer digits, nothing is hidden.

Constraints: 0 ≤ len(text) ≤ 100. Digits are the characters "0" to "9".

Example: solution("1234-5678-9012") returns "****-****-9012".

Exercise 3

A stopwatch counts whole seconds. Write solution(seconds) to show them as a clock string "H:MM:SS".

Rules:

  • Hours are not padded and may exceed 24 (a stopwatch does not roll over into days).
  • Minutes (0-59) and seconds (0-59) are always two digits, padded with a zero.

Constraints: 0 ≤ seconds ≤ 10^7.

Example: solution(3725) returns "1:02:05" (1 hour, 2 minutes, 5 seconds).

Exercise 4

A label printer prints words on lines that are at most width characters wide. Given the list words, place them greedily: put each word on the current line if it fits, otherwise start a new line.

Return the lines as a list of strings.

Rules:

  • Words on the same line are separated by exactly one space. A line has no leading or trailing spaces.
  • A word fits when the line, a space and the word together have at most width characters. The first word of a line always goes on it.
  • Every word is at most width characters long. No words gives [].

Constraints: 0 ≤ len(words) ≤ 1000, 1 ≤ width ≤ 100.

Example: solution(["the", "cat", "sat", "down"], 7) returns ["the cat", "sat", "down"] because "sat down" would be 8 characters.

The explanation and tasks are public. Running this lesson requires an account with course access.

Practice

Loading Python…
More
Loading exercise content…
Visualizer Watch your code run line by line

Run your code, then choose Play to follow its execution.

Memory · names → values
Variables appear here as Python creates them.
Output & plots

One-line daily quest