Skip to content

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.

Expected output
Honda Civic
11
Need a hint?

Join with + plus a " " space string; len() of "Honda Civic" is 11 because the space counts.

Show the solution
s03_workshop01_sol_making_strings.py
brand = "Honda"
model = "Civic"
label = brand + " " + model
print(label)
print(len(label))

View the full solution file on GitHub

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).

Expected output
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
s03_workshop02_sol_fstrings.py
brand = "Honda"
model = "Civic"
year = 2022
print(f"{brand} {model} ({year})")

View the full solution file on GitHub

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".
Expected output
TN09XY5678
TN09xy5678
Need a hint?

.upper() returns an all-caps copy and .replace("tn", "TN") swaps only the matched part.

Show the solution
s03_workshop03_sol_string_methods.py
plate = "tn09xy5678"
# Problem-03 Task-1: print the plate in upper case
print(plate.upper())

# Problem-03 Task-2: print plate with "tn" replaced by "TN"
print(plate.replace("tn", "TN"))

View the full solution file on GitHub

Problem-2: Plate Digits.

Write a program that prints whether "5678" is all digits and whether "tn09" is all digits.

Expected output
True
False
Need a hint?

.isdigit() is True only when every character is a digit, so "tn09" is False.

Show the solution
s03_workshop03_sol_string_methods.py
print("5678".isdigit())
print("tn09".isdigit())

View the full solution file on GitHub

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).
Expected output
Civic
2022
LX
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
s03_workshop04_sol_indexing_slicing.py
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)

View the full solution file on GitHub

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.

Get the Video Course

Workshop: Variables and Data Types Next: Workshop: Operators Booleans and Conditionals