The request

From Thomas Whitaker, Head of Merchandising
Your goal
Set result to the string LINEN THROW PILLOW | $34.00, built from name and price: strip the spaces, uppercase the name, and format the price with exactly 2 decimals.
- Store a text value (str) in
resultnot done yet
Your code
Loading the editor…
Reference solution
Mote checks your result
Press Run. Each row that matches the goal becomes a firefly in your lantern.
What Thomas gets, and why
The lesson
Strings are text
A string (str) is text between quotes. Single and double quotes do the same thing:
product = "Linen Throw Pillow"
city = 'Portland'
Analysts spend a lot of time on strings, because real data is full of messy text: names typed by customers, codes exported from old systems, cities with trailing spaces.
String methods
A method is a function that belongs to a value. You call it with a dot. Strings come with many useful ones:
raw = " linen throw pillow "
raw.strip() # 'linen throw pillow' spaces removed at both ends
raw.upper() # ' LINEN THROW PILLOW '
raw.strip().title() # 'Linen Throw Pillow'
"SKU-1042".lower() # 'sku-1042'
"oak, walnut".replace(",", " /") # 'oak / walnut'
"Oak Side Table".startswith("Oak") # True
Methods return a new string. The original is never changed, because strings in Python cannot be modified in place. If you want to keep the cleaned version, store it:
name = raw.strip()
You can chain methods, and they run left to right: raw.strip().upper() strips first, then
uppercases the result.
Joining text with f-strings
To build a sentence or label from values, put an f before the opening quote and write variables
inside curly braces:
name = "Oak Side Table"
stock = 14
f"{name}: {stock} in stock" # 'Oak Side Table: 14 in stock'
Anything inside the braces is evaluated as Python, so you can call methods there too:
f"{name.upper()}".
Formatting numbers
After the value, a colon starts a format spec. The ones you’ll use constantly:
price = 34
f"{price:.2f}" # '34.00' two decimals
f"{1249.5:,.2f}" # '1,249.50' thousands separator
f"{0.237:.1%}" # '23.7%' as a percentage
.2f means “fixed point, two decimals”. It also rounds: f"{49.996:.2f}" gives '50.00'.
Note the dollar sign in a label is just a character in the string, so f"${price:.2f}" prints
$34.00.
Why not just use +?
You can join strings with +, but every piece must already be a string:
"Price: " + 34 # TypeError: can only concatenate str (not "int") to str
"Price: " + str(34) # 'Price: 34'
f-strings convert values for you and let you format them, so they are the modern standard.
Why analysts clean text first
Text that looks identical to a person can be different to a computer. "Portland", "portland" and
"Portland " are three different values, so a count of customers per city would split one city into
three rows. Cleaning text into one consistent form, usually stripped and in a fixed case, is one of
the first jobs on almost every dataset. You’ll do it at scale with pandas in the cleaning chapter,
using exactly the methods you just learned.
Common mistakes
- Calling
name.strip()and expectingnameitself to change. Strings are immutable; use the returned value. - Formatting the price with
{price}instead of{price:.2f}, which prints34rather than34.00. - Forgetting the
fbefore the quote. Without it, Python prints the braces literally:"{name}"stays"{name}".
Lantern lit!
Practised: stringsstripupperf stringsnumber formatting
- Solved
- No hints
- First run
