σμimplied vol · KKmax(S−K,0)udβρᵢⱼfat tailsPnL · drawdownyield · TdSₜ = μSₜ dt + σSₜ dWₜC = S·N(d₁) − K·e⁻ʳᵀ·N(d₂)σₚ² = wᵀΣwd₁ = [ln(S/K) + (r + σ²⁄2)T] ⁄ σ√T∂V/∂t + ½σ²S²·∂²V/∂S² + rS·∂V/∂S − rV = 0Sharpe = (Rₚ − R_f) ⁄ σₚVaR₉₉ = μ − 2.33σρ = Cov(X,Y) ⁄ σₓσᵧE[Rₚ] = Σ wᵢ·E[Rᵢ]φ(x) = e^(−x²/2) ⁄ √2πE[Rᵢ] = R_f + βᵢ(E[Rₘ] − R_f)dXₜ = θ(μ − Xₜ)dt + σ dWₜσ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁C − P = S − K·e⁻ʳᵀΔ = ∂V/∂S Γ = ∂²V/∂S²p = (e^(rΔt) − d) ⁄ (u − d)rₜ = ln(Pₜ ⁄ Pₜ₋₁)E[Xₜ₊₁ | ℱₜ] = Xₜf* = (bp − q) ⁄ bz = (x − μ) ⁄ σP(N=k) = λᵏe^(−λ) ⁄ k!Θ = −∂V/∂Tν = S·φ(d₁)·√Tmin wᵀΣw s.t. wᵀμ = μ*∫Σ∂∞λ
uantCraft AcademyRoadmapsSkills
Ctrl K
AO
+1.2k Learners
LoginJoin ↗
RoadmapsSkills
AO
+1.2k Learners
Login
  • English
  • Français
  • Español
  • العربية
  • Português
  • Deutsch
  • 日本語
  • 中文
σμimplied vol · KKmax(S−K,0)udβρᵢⱼfat tailsPnL · drawdownyield · TdSₜ = μSₜ dt + σSₜ dWₜC = S·N(d₁) − K·e⁻ʳᵀ·N(d₂)σₚ² = wᵀΣwd₁ = [ln(S/K) + (r + σ²⁄2)T] ⁄ σ√T∂V/∂t + ½σ²S²·∂²V/∂S² + rS·∂V/∂S − rV = 0Sharpe = (Rₚ − R_f) ⁄ σₚVaR₉₉ = μ − 2.33σρ = Cov(X,Y) ⁄ σₓσᵧE[Rₚ] = Σ wᵢ·E[Rᵢ]φ(x) = e^(−x²/2) ⁄ √2πE[Rᵢ] = R_f + βᵢ(E[Rₘ] − R_f)dXₜ = θ(μ − Xₜ)dt + σ dWₜσ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁C − P = S − K·e⁻ʳᵀΔ = ∂V/∂S Γ = ∂²V/∂S²p = (e^(rΔt) − d) ⁄ (u − d)rₜ = ln(Pₜ ⁄ Pₜ₋₁)E[Xₜ₊₁ | ℱₜ] = Xₜf* = (bp − q) ⁄ bz = (x − μ) ⁄ σP(N=k) = λᵏe^(−λ) ⁄ k!Θ = −∂V/∂Tν = S·φ(d₁)·√Tmin wᵀΣw s.t. wᵀμ = μ*∫Σ∂∞λ
uantCraft AcademyRoadmapsSkills
Ctrl K
AO
+1.2k Learners
LoginJoin ↗
RoadmapsSkills
AO
+1.2k Learners
Login
  • English
  • Français
  • Español
  • العربية
  • Português
  • Deutsch
  • 日本語
  • 中文
Python skill›Module 1 · Python foundations for quants›Topic 1.3·~30 minBeginner

Strings and f-strings

Skill positionModule 1 of 11 · Topic 3 of 33
1.3 Strings and f-strings
—Lab score— / 100Total XP—Streak—
Lecture0% read

2 min readCode examples: 6

  1. 01Text is a string
  2. 02Indexing and slicing
  3. 03Methods that clean and split text
  4. 04f-strings put values into text
  5. 05Format specifications
01

Text is a string

A string (str) is text. Write it between single or double quotes: both mean the same, so pick one style and keep it. A backslash starts an escape, such as \n for a new line. len() counts the characters. Strings are immutable: you never change one in place, you build a new one.

Python
1
2
3
4symbol = "AAPL"
note = 'bid and ask'
print(symbol, len(symbol))  # AAPL 4
print(note)                 # bid and ask
02

Indexing and slicing

Each character has a position, counting from 0. text[0] is the first character and text[-1] the last. A slice text[start:stop] takes the characters from start up to, but not including, stop. Leave one side empty to run to that end.

Python
1
2
3
4# Indexing starts at 0; a slice stops before its end index
ticker = "AAPL.US"
print(ticker[0], ticker[-1])   # A S
print(ticker[:4], ticker[5:])  # AAPL US
03

Methods that clean and split text

Methods are functions attached to a value and called with a dot. They return a new string and leave the original alone. strip() removes spaces and newlines around the text, upper() and lower() change the case, replace(old, new) swaps text, and split(",") cuts a line at every comma into a list of pieces. Data feeds are full of lines like this.

Python
1
2
3
4
5
6# Clean up a raw line from a data feed
raw = "  aapl,184.25,200 \n"
parts = raw.strip().split(",")
print(parts)             # ['aapl', '184.25', '200']
print(parts[0].upper())  # AAPL
print(float(parts[1]))   # 184.25
Python
1
2
3# join is the opposite of split; in tests for a piece of text
print("-".join(["AAPL", "MSFT"]))  # AAPL-MSFT
print("AAPL" in "AAPL,MSFT")       # True
04

f-strings put values into text

Put an f before the opening quote and write any expression between { and }: Python evaluates it and inserts the result as text. This replaces gluing pieces together with + and converting numbers by hand. Anything that is a valid expression works inside the braces, even arithmetic.

Python
1
2
3
4# f-strings put values straight into text
symbol, bid, ask = "AAPL", 184.25, 184.5
print(f"{symbol} {bid} x {ask}")       # AAPL 184.25 x 184.5
print(f"{symbol} spread {ask - bid}")  # AAPL spread 0.25
05

Format specifications

After a colon inside the braces, a format specification controls how the value looks. .2f shows two decimals, , adds thousands separators, + always shows the sign, >8 right-aligns in a width of 8, and .1% shows a fraction as a percentage. They combine: +,.2f is a signed amount with separators and two decimals.

Python
1
2
3
4
5
6# After the colon, a format spec controls the look
price, pnl, quantity = 184.5, 1234.5, 200
print(f"{price:.2f}")       # 184.50
print(f"{pnl:+,.2f}")       # +1,234.50
print(f"{quantity:>8d}|")   # "     200|"
print(f"{0.0123:.1%}")      # 1.2%
Format for display only

A formatted value is text, not a number any more. Keep calculating with the numbers (see integers and floats) and format only at the moment you print or store text.

Next up: booleans and comparisons, the True and False values that every decision in a trading rule is built from.

Lab · what's graded

4 tasks · 100 pts
  • Write a quote as text with quote_line25 pts
  • Show profit and loss with a sign using signed_pnl25 pts
  • Clean a ticker symbol with normalize_symbol25 pts
  • Read a fill line from a feed with parse_fill25 pts
Explorer · lab-3-1
▾ lab-3-1
…
bash · in-browser
learner@quantcraft:~/lab-3-1$
Python not loaded yetPythonLn 1, Col 1Spaces: 4UTF-8

Reset this topic?

Everything in lab-3-1 goes back to the starter files and the terminal is cleared. Your current solution will be lost — this can't be undone.

Will be reset
  • main.py → starter code
  • Terminal history and test output
  • Unsubmitted changes
Will be kept
  • Best lab score
  • Your XP and streak
  • Lecture progress and notes