Workshop: Strings and f-strings (Optional, Self-Study)¶
View workshop code on GitHub Get the Video Course
From the lesson
Workshop problems for Section 03 Concepts. Build each yourself first, then expand the solution to check.
Four optional workshop problems, one per concept step, in a car scenario. The numbers line up with the steps. Self-study: the stubs say WHAT to do; check python-workshop/solutions.
Step-01: Making Strings¶
Concepts: joining strings with +, len().
Write a program that joins a brand (Honda) and a model (Civic) into a label with a space between them, prints the label, and prints its length.
Need a hint?
Join with + plus a " " space string; len() of "Honda Civic" is 11 because the space counts.
Show the solution
Pages for this step: Concept | Practice | Workshop
Python Files for this step: Concept | Practice | Practice Solutions | Workshop | Workshop Solutions
Step-02: f-strings¶
Concepts: f-strings.
Write a program that uses a brand (Honda), a model (Civic), and a year (2022) with an f-string to print Honda Civic (2022).
Need a hint?
An f-string drops brand, model, and year into the text; type the literal parentheses around {year} yourself.
Show the solution
Pages for this step: Concept | Practice | Workshop
Python Files for this step: Concept | Practice | Practice Solutions | Workshop | Workshop Solutions
Step-03: String Methods¶
Concepts: upper(), replace().
Warning
.upper() and .replace() do not change plate in place. Strings are immutable, so each call returns a NEW string. Print the returned value (or store it in a new variable); plate itself stays "tn09xy5678".
Problem-1: Plate Case.
Write a program that, for a plate "tn09xy5678":
- Task-1: print it in UPPER CASE.
- Task-2: print it again with
"tn"replaced by"TN".
Need a hint?
.upper() returns an all-caps copy and .replace("tn", "TN") swaps only the matched part.
Show the solution
Problem-2: Plate Digits.
Write a program that prints whether "5678" is all digits and whether "tn09" is all digits.
Need a hint?
.isdigit() is True only when every character is a digit, so "tn09" is False.
Show the solution
Pages for this step: Concept | Practice | Workshop
Python Files for this step: Concept | Practice | Practice Solutions | Workshop | Workshop Solutions
Step-04: Indexing and Slicing¶
Concepts: slicing.
Write a program that, for a car code "Civic2022LX", using slices:
- Task-1: print the model (
Civic, the first 5 letters). - Task-2: print the year (
2022, the four characters in the middle). - Task-3: print the trim (
LX, the rest at the end).
Need a hint?
code[:5] slices the first five (the model); code[5:9] slices the middle four (the year); code[9:] takes the rest (the trim).
Show the solution
code = "Civic2022LX"
# Task-1: print the model (first 5 letters)
print(code[:5]) # Civic (the model)
# Task-2: print the year (four middle characters)
print(code[5:9]) # 2022 (the year)
# Task-3: print the trim (the rest at the end)
print(code[9:]) # LX (the trim)
Pages for this step: Concept | Practice | Workshop
Python Files for this step: Concept | Practice | Practice Solutions | Workshop | Workshop Solutions
Finished this section?
Check your work against the solutions above, then head back to the Section 03 lesson, or try the Section 03 practice for more reps.
Prefer to learn by watching?
The full video course teaches every concept on screen and builds the projects with you, step by step.
Workshop: Variables and Data Types Next: Workshop: Operators Booleans and Conditionals