12. Strings — Homework solutions
The .py solution files are in
exercises/12/homework/solutions/.
Problem 1 — Loud and quiet
Problem. Print a name three times: original, upper, lower.
How to think about it. You need two string methods:
.upper() and .lower(). Neither changes the
original variable, so all three prints reference the same
name.
Worked solution.
name = "Keiko"
print(name)
print(name.upper())
print(name.lower())Common mistakes.
- Writing
name = name.upper()between prints. That overwrites the original value, so a later print uses the changed text instead of"Keiko". Keep the original untouched and only transform it inside each print call.
Problem 2 — Stat line
Problem. Use an f-string to produce
Keiko Lv 7 HP 95.
How to think about it. Three values, three
{} slots in the f-string: one for the name, one for the
level, one for HP. The labels (Lv, HP) and the
spaces between fields live inside the f-string itself.
Worked solution.
name = "Keiko"
level = 7
hp = 95
print(f"{name} Lv {level} HP {hp}")The double-space after {name} and the triple-space after
the level match the requested output. Adjust the spaces to taste.
Common mistakes.
- Forgetting the
fprefix before the opening quote. Without it,{name}is printed literally as the text{name}, not the variable's value. - Using
+to join a string and an integer. Python raises aTypeError. F-strings handle the conversion automatically.
Problem 3 — One-line three-line poem
Problem. One print call, three lines of
output, separated by \n.
How to think about it. Inside a regular string,
\n is the newline character, so one string with two
\ns prints as three lines. The comment has to say
why: print writes whatever it is handed, and this
string already contains newline characters.
Worked solution.
print("Code is poetry,\nbugs are typos,\nfix them and ship.")
# One print call still produced three lines because the string itself
# contained two \n characters, each of which Python replaces with a
# newline before sending to the terminal.Common mistakes.
- Writing
\\ninstead of\n. The double backslash means a literal backslash followed by ann, which Python does not treat as a newline.
Challenge — Title block
Problem. Print a title with a dashed border above and below. The border length is computed from the title length, not typed out.
How to think about it. The border has to be
len(title) + 4 characters long (two dashes on each side of
" TITLE "). Build a row of dashes that length with
"-" * n, and build the middle row with an f-string.
Worked solution.
title = "INVENTORY"
border = "-" * (len(title) + 4)
middle = f"-- {title} --"
print(border)
print(middle)
print("Inside text goes here")
print(middle)
print(border)The output for title = "INVENTORY":
-------------
-- INVENTORY --
Inside text goes here
-- INVENTORY --
-------------
Change title to "SHOP" and the border
shrinks. That is why you use len(title) instead of counting
dashes by hand.
Common mistakes.
- Hard-coding the number of dashes. The challenge is to compute the
length from
len(title). - Forgetting the parentheses:
"-" * len(title) + 4adds the integer4to a string, which is aTypeError. Write"-" * (len(title) + 4).
Done?
Next is Numbers and math, then Getting input, then the Part 3 mini-project.