flowchart TD
R["raw_clinic = ' dhaka_med '<br/>(Immutable Original str)"]
S1[".strip() -> New str: 'dhaka_med'"]
S2[".replace('_', ' ') -> New str: 'dhaka med'"]
S3[".title() -> New str: 'Dhaka Med'"]
C["clean_clinic (Final Bound Name Tag)"]
R --> S1 --> S2 --> S3 ==> C
3 Strings, Text Processing, Formatting, and User Input
Chapter Mission: Master Python’s
strtype from first principles—zero-based and negative indexing, half-open slicing, string immutability, method chaining, splitting delimited clinical records, publication-grade f-string formatting, and safe handling of interactiveinput()—so messy real-world health records never break your analysis pipeline.
Chapter Companion Notebooks (1-to-1 Mirrored in Google Colab)
| Lab Type | Interactive Colab Notebook | Scan (Print) |
|---|---|---|
| Walkthrough | ch03 / 01_walkthrough.ipynb | ![]() |
| Guided To-Dos | ch03 / 02_guided_todos.ipynb | ![]() |
| Coding Exam | ch03 / 03_coding_exam.ipynb | ![]() |
Learning Objectives
By the end of this chapter, you will be able to:
- Extract structured fields from clinical IDs and barcodes using 0-based indexing, negative indexing, and half-open slicing (
s[start:stop]). - Explain string immutability and avoid the two classic beginner traps: trying to mutate a string in place (
TypeError) and calling.strip()without reassigning the result. - Clean, normalize, search, split, and reassemble messy health-survey text using chained string methods (
.strip(),.replace(),.title(),.upper(),.split(),.join()). - Format numbers, percentages, thousands separators, zero-padded IDs, and aligned console tables cleanly using publication-grade f-strings.
- Sanitize and type-cast raw text coming from interactive
input()prompts (and explain why automated notebooks simulateinput()for reproducibility). - Build end-to-end text-cleaning mini-pipelines that turn dirty field-survey payloads into clean biostatistics summaries.
3.1 Stage 1 — First-Principles Intuition: Laminated Hospital Wristbands
In public health and clinical research, over 70% of your real-world cleaning time happens on text: misspelled district names (" sylhet_sadar "), pipe-delimited hospital lab feeds ("PAT_109 | 38.4 C | pos"), structured specimen barcodes ("BD-CTG-2026-0094-MAL"), and blood pressure readings stuck together with a slash ("148/92 mmHg").
Before writing a single string method, anchor two permanent mental rules:
Picture a sealed, laminated patient wristband printed with a 16-character barcode (BD-DHK-2026-0042). Two physical laws govern every Python str object:
- Every Character Has Two Addresses: Counting from the left starts at
0(not1!), so the last character sits atlen(s) - 1. Counting backwards from the right starts at-1(-1is the final character,-2is second-to-last, and so on). - The Band Is Permanently Laminated (Immutable): Once a
strobject is created in memory, you can never scratch out or overwrite a single character inside it. If you want to fix a typo or strip spaces, Python prints a brand-new laminated wristband in a new memory slot—and unless you attach a variable name tag (clean = ...) to that new band, it falls straight into the trash!
3.1.1 Why Half-Open Slicing ([start:stop]) Is Brilliant
When slicing s[start:stop], Python includes the character at start and stops right before stop (excluding stop). Mathematicians write this as the half-open interval \([ \text{start}, \text{stop} )\). That single design choice gives you two superpowers:
- Instant Length Formula: The length of
s[start:stop]is alwaysstop - start(e.g.,patient_id[3:6]has exact length \(6 - 3 = 3\) characters:"DHK"). - Seamless Adjacent Splits:
s[:k] + s[k:]always reconstructs the exact original stringswith zero overlap and zero missing characters!
3.2 Stage 2 — Visual Mental Model: Immutable String Transformation Pipeline
Watch what happens in memory when you chain .strip().replace("_", " ").title() onto a messy clinic string:
Notice the crucial insight: raw_clinic itself never changes! Each method in the chain returns a brand-new str object, passes it straight to the next method on the right, and clean_clinic = ... binds our new name tag to the final cleaned object.
3.3 Stage 3 — Line-by-Line Code Walkthrough & Error Anatomy
Open part_01_python_foundations/ch03_strings_input/01_walkthrough.ipynb in Colab and execute every cell alongside the dissections below.
3.3.1 Indexing, Negative Indexing & Half-Open Slicing
Look closely at the exact character coordinates of "BD-DHK-2026-0042" (length 16):
| Char | B |
D |
- |
D |
H |
K |
- |
2 |
0 |
2 |
6 |
- |
0 |
0 |
4 |
2 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Idx | 0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
| Neg | -16 |
-15 |
-14 |
-13 |
-12 |
-11 |
-10 |
-9 |
-8 |
-7 |
-6 |
-5 |
-4 |
-3 |
-2 |
-1 |
patient_id = "BD-DHK-2026-0042"
print("Length:", len(patient_id))
print("First char [0]:", patient_id[0], "| Last char [-1]:", patient_id[-1])
print("Country [:2]:", patient_id[:2])
print("District [3:6]:", patient_id[3:6])
print("Year [7:11]:", patient_id[7:11])
print("Serial [-4:]:", patient_id[-4:])Expected Output:
Notice the two convenient slice shortcuts: omitting start ([:2]) starts from index 0, and omitting stop ([-4:]) slices all the way through the final character!
3.3.2 String Immutability & Method Chaining
raw_clinic = " dhaka_medical_college "
# Calling a method WITHOUT reassignment discards the cleaned copy!
raw_clinic.strip()
print("Unchanged raw_clinic:", repr(raw_clinic))
# Capture the new string object with assignment + method chaining
clean_clinic = raw_clinic.strip().replace("_", " ").title()
print("Cleaned clinic:", repr(clean_clinic))
print("Starts with 'Dhaka'?:", clean_clinic.startswith("Dhaka"))
print("Count of 'a':", clean_clinic.lower().count("a"))Expected Output:
When debugging messy survey text, print(" Dhaka ") hides trailing spaces visually. Wrapping any string in repr(s) displays explicit quotes and escape characters (' Dhaka ', 'line1\n'), exposing invisible whitespace bugs in one second!
Here is your everyday Public Health string-method reference card:
| Method / Operator | Purpose | Example |
|---|---|---|
.strip() |
Remove leading & trailing whitespace | " 42 \n".strip() \(\rightarrow\) "42" |
.upper() / .lower() |
Standardize letter casing | "dhk".upper() \(\rightarrow\) "DHK" |
.title() |
Capitalize first letter of each word | "sylhet sadar".title() \(\rightarrow\) "Sylhet Sadar" |
.replace(a, b) |
Replace all occurrences of a with b |
"38.4 C".replace("C", "") \(\rightarrow\) "38.4 " |
.startswith(p) / .endswith(s) |
Check prefix or suffix (bool) |
"BD-MAL".endswith("MAL") \(\rightarrow\) True |
sub in text |
Membership test (bool) |
"pos" in "ns1_pos" \(\rightarrow\) True |
.split(sep) |
Split text into a list of tokens | "148/92".split("/") \(\rightarrow\) ["148", "92"] |
sep.join(items) |
Join strings with delimiter sep |
",".join(["A", "B"]) \(\rightarrow\) "A,B" |
3.3.3 Splitting Delimited Records & Joining Fields
Hospital lab systems (such as HL7 feeds) frequently separate columns with pipe symbols (|) or semicolons (;) instead of commas. Calling .split("|") breaks one raw row into indexable pieces (parts[0], parts[1], …), and ",".join([...]) reassembles clean string tokens into a standard CSV line:
raw_row = " PAT-019 | 31 | 10.4 g/dL | positive "
parts = raw_row.split("|")
pat_id = parts[0].strip()
age = int(parts[1].strip())
hb = float(parts[2].replace("g/dL", "").strip())
status = parts[3].strip().upper()
# Reassemble a clean CSV row with .join()
clean_csv = ",".join([pat_id, str(age), str(hb), status])
print("Clean CSV row:", clean_csv)Expected Output:
Notice that ",".join(...) requires every item in the list to be a str—that is why we wrap str(age) and str(hb) before joining!
3.3.4 Publication-Grade f-String Formatting
When reporting epidemiological tables or manuscript statistics, raw floating-point prints like 0.02233062330623306 look amateurish. Python f-strings accept a mini-language after a colon : inside {...} to format numbers with journal-grade precision:
| Format Specifier | Meaning | Input \(\rightarrow\) Output |
|---|---|---|
:.1f / :.2f |
Fixed decimal places | 128.467 \(\rightarrow\) 128.5 |
:, |
Thousands comma separator | 18450 \(\rightarrow\) 18,450 |
:.2% |
Multiply by \(100\) & append % |
0.02233 \(\rightarrow\) 2.23% |
:04d |
Zero-padded integer (width 4) | 42 \(\rightarrow\) 0042 |
:<14 / :>10 |
Left- or right-align in fixed width | "Site" \(\rightarrow\) "Site " |
site = "Chattogram"
seq_num = 42
screened = 18450
cases = 412
positivity = cases / screened
mean_sbp = 128.467
print(f"Facility ID : CTG-{seq_num:04d}")
print(f"Screened : {screened:,} adults")
print(f"Positivity : {positivity:.2%} ({cases} / {screened:,})")
print(f"Mean SBP : {mean_sbp:.1f} mmHg")
print(f"{'Site':<14} | {'Screened':>10} | {'Positivity':>10}")
print(f"{site:<14} | {screened:>10,} | {positivity:>10.2%}")Expected Output:
Python’s % format specifier already multiplies by 100! If positivity = 0.02233, writing {positivity:.2%} gives 2.23%. Writing {positivity * 100:.2%} accidentally multiplies by \(100\) twice and prints 223.31%!
3.3.5 Interactive User Input (input()) & Validation Hygiene
In an interactive Python script or Colab cell, input("Prompt: ") pauses execution, waits for the user to type text and press Enter, and always returns a str—even when the user types 38.4 or 28.
Because interactive input() pauses execution forever in automated research pipelines and cloud notebook runners, disciplined researchers isolate raw input at the top of a cell (or simulate it cleanly during automated testing):
# In an interactive Colab session, you can uncomment the line below:
# raw_temp = input("Enter patient temperature (e.g., 38.4 C): ")
# For reproducible, non-blocking execution, we simulate the user's text:
raw_temp = " 38.4 C "
print("Raw input type:", type(raw_temp), "| Value:", repr(raw_temp))
temp_c = float(raw_temp.replace("C", "").strip())
has_fever = temp_c >= 38.0
print(f"Parsed Temp: {temp_c:.1f} C | Fever Flag: {has_fever}")Expected Output:
Golden Rule of User Input: Treat every string returned by input() as untrusted messy text—always .strip() whitespace, strip expected units, and explicitly cast to int or float before arithmetic!
3.3.6 Break It On Purpose! (String Error Anatomy)
Python’s Traceback:
Diagnosis: len("DHK-042") is 7, which means valid positive indices run from 0 through 6 (len(code) - 1). Index 7 steps one slot past the end of the string!
Fix: Use negative indexing code[-1] (always the last character, regardless of length) or code[len(code) - 1].
Python’s Traceback:
Diagnosis: Python strings are immutable—you cannot modify individual characters in place.
Fix: Construct a brand-new string via slicing/concatenation (patient_id = "U" + patient_id[1:]) or .upper() (patient_id = patient_id.upper()).
Python’s Traceback:
Diagnosis: float() tolerates outer spaces (float(" 38.6 ") succeeds), but crashes on non-numeric unit suffixes like "C", "kg", or "mmHg".
Fix: Strip the unit first: temp_c = float(raw_temp.replace("C", "").strip()).
3.4 Stage 4 — The Researcher’s Problem-Structuring Blueprint
Scenario: A rural community health worker texts a rapid dengue triage SMS to the district surveillance desk: raw_sms = " barishal_sadar | 007 | 39.1 C | 112000 /uL ". The four pipe-delimited fields represent upazila name, patient serial number, body temperature (\(^\circ\text{C}\)), and platelet count (normal \(\ge 150{,}000\text{ /}\mu\text{L}\)). Parse all fields cleanly, generate a zero-padded case ID ("BRS-0007"), evaluate urgent_referral (True if febrile \(\ge 38.0^\circ\text{C}\) and platelets \(< 150{,}000\)), and print a clean two-line clinical alert.
3.4.1 Blueprint (Completed on Paper Before Coding)
- Inputs & Units: One raw
strcontaining 4|-separated fields: messy text (upazila), zero-padded integer (serial), float with"C"unit (temp_c), and integer with"/uL"unit (platelets). - Edge Cases & Traps:
- Leading/trailing spaces inside every token after
.split("|"). - Unit suffixes (
"C","/uL") will triggerValueErrorif not removed beforefloat()/int(). - Calling
int("007")turns"007"into7; we must re-pad with:04dto format"BRS-0007".
- Leading/trailing spaces inside every token after
- Step-by-Step Algorithm:
- Split
raw_smson"|"intoparts. - Clean
parts[0]with.strip().replace("_", " ").title(). - Parse
serial,temp_c, andplateletsafter removing units and whitespace. - Build
case_id = f"BRS-{serial:04d}", compute boolean flags, and print formatted alert lines.
- Split
- Verification Plan (Hand Check):
upazila\(\rightarrow\)"Barishal Sadar";case_id\(\rightarrow\)"BRS-0007".39.1 >= 38.0(True) and112000 < 150000(True) \(\Rightarrow\)urgent_referral = True.112000with:,prints"112,000".
3.4.2 Clean Python Implementation (Verbatim in 01_walkthrough.ipynb)
# Step 1: Raw incoming clinic SMS text
raw_sms = " barishal_sadar | 007 | 39.1 C | 112000 /uL "
# Step 2: Split on '|' and sanitize each text field
parts = raw_sms.split("|")
upazila = parts[0].strip().replace("_", " ").title()
serial = int(parts[1].strip())
temp_c = float(parts[2].replace("C", "").strip())
platelets = int(parts[3].replace("/uL", "").strip())
# Step 3: Derive clinical triage flags & standardized ID
case_id = f"BRS-{serial:04d}"
is_febrile = temp_c >= 38.0
low_platelets = platelets < 150000
urgent_referral = is_febrile and low_platelets
# Step 4: Format publication-clean clinical alert
print(f"[{case_id}] {upazila} | Temp: {temp_c:.1f} C")
print(f"Platelets: {platelets:,} /uL | Urgent Referral: {urgent_referral}")Output:
3.5 Stage 5 — Guided “To-Do” Practice Labs
Open part_01_python_foundations/ch03_strings_input/02_guided_todos.ipynb in Google Colab, run Step 0 to activate sharukh-tutor, and complete all six exercises below:
- To-Do 3.1 (Warm-Up — Cleaning Messy Clinic District Names): Turn
raw_district = " sylhet_sadar "intoclean_district("Sylhet Sadar") and computechar_count = len(clean_district)(12). - To-Do 3.2 (Indexing & Slicing — Structured Patient IDs): From
patient_id = "BD-DHK-2026-0482", slicecountry_code("BD"),district_code("DHK"),cohort_year(2026,int), andserial_str("0482",str). - To-Do 3.3 (Splitting Delimited Text — Pipe-Delimited Lab Record): Split
raw_record = " PAT_109 | 38.4 C | dengue_ns1_pos "on"|"to extractlab_id("PAT_109"),temp_c(38.4), andis_dengue_pos(True). - To-Do 3.4 (Immutability & Rebuild — Standardizing Facility Codes): From
raw_code = "uhc_0042", buildfixed_code("UHC-0042") andreplaced_facility("DHK-0042") without mutating in place. - To-Do 3.5 (Publication Formatting — Epidemiology f-String): Format
district = "Rajshahi",screened = 12450,positive = 386,prevalence = positive / screened,mean_hb = 11.246into"Rajshahi: n=12,450 | Pos=386 (3.10%) | Mean Hb=11.2 g/dL". - To-Do 3.6 (Simulated
input()Hygiene — Clinic Intake & Zero-Padded Tag): Cleanraw_name = " fatema akter ",raw_age = " 28 ",raw_seq = " 7 ",raw_temp = " 38.6 C "intoclean_name,age_int,temp_c, andcase_tag("CASE-0007").
3.6 Stage 6 — Concept & Execution Trace Test (No Computer Allowed!)
Predict every output on paper before peeking at Stage 8:
Zero-Based Bounds: If
code = "SYL-019", what doeslen(code)return, and what exact exception doescode[7]raise?Half-Open Slicing: Given
s = "EPIDEMIOLOGY", what exact substring doess[0:3]return? What abouts[-4:]?Silent Immutability Bug: After running:
What exact string prints, and why?
In-Place Edit Trap: Why does
tag = "uhc-01"; tag[0] = "U"fail, and what exact exception name does Python raise?Leading Zero Preservation: What is the difference between
serial_str = "0042"andserial_int = int("0042")?Split Return Type: What exact list does
"148/92".split("/")produce—are the elements integers or strings?Percentage Formatter Trap: If
rate = 0.045, what doesf"{rate:.1%}"produce? What mistake happens if you writef"{rate * 100:.1%}"?Zero-Padding Format Specifier: What exact string does
f"PAT-{19:05d}"evaluate to?Interactive
input()Return Type: If a clinician types37.8at aninput("Temp: ")prompt, what doestype(val)return?Execution Trace: Trace
resultafter these three lines:
3.7 Stage 7 — Chapter 3 Hands-On Coding Exam (100 Points)
Open part_01_python_foundations/ch03_strings_input/03_coding_exam.ipynb, solve all three tasks, and run tutor.check_exam("ch03", exam_submissions) to score your work out of 100 points.
3.7.1 Exam Task 3.1 (30 Points): Clinical Specimen Barcode Parser
A surveillance laboratory scans a messy barcode string: raw_barcode = " bd-ctg-2026-0094-mal ". Standardize and extract with exact required types:
clean_barcode: stripped and uppercased ("BD-CTG-2026-0094-MAL",str)division_code: 3-letter division code ("CTG",str)sample_year: 4-digit collection year (2026,int)sample_number: integer specimen sequence number (94,int)is_malaria_panel:Trueifclean_barcodeends with"MAL"(bool)
3.7.2 Exam Task 3.2 (35 Points): Dirty Field-Survey Intake Cleaner
A mobile data-collection form sends a single semicolon-delimited text payload: raw_payload = " rahim_uddin ; 45 yrs ; 72.4 kg ; 168.0 cm ; 148/92 mmHg ". Parse and compute:
patient_name: stripped, underscores replaced with spaces, Title Case ("Rahim Uddin",str)age_years: integer age (45,int)bmi: float BMI (\(\text{weight\_kg} / \text{height\_m}^2\)) rounded to 2 decimal places (25.65,float)sbp_mmhg: systolic blood pressure (number before"/") asint(148)dbp_mmhg: diastolic blood pressure (number after"/") asint(92)
3.7.3 Exam Task 3.3 (35 Points): Publication-Ready Surveillance Bulletin Generator
Given district outbreak summary variables facility_code = "uhc-syl-03", report_seq = 14, population = 340500, confirmed_cases = 512, and deaths = 9, compute and format:
report_id: uppercase facility code +"-R"+ zero-padded 4-digitreport_seq("UHC-SYL-03-R0014")incidence_per_100k:(confirmed_cases / population) * 100000rounded to 2 decimals (150.37)cfr_pct_str: Case Fatality Rate (deaths / confirmed_cases) formatted as a 2-decimal percentage string ("1.76%")bulletin_line: exact formatted summary string:"[UHC-SYL-03-R0014] Pop=340,500 | Cases=512 (150.4/100k) | CFR=1.76%"
3.8 Stage 8 — Complete Annotated Solutions & Debugging Post-Mortem
3.8.1 A. Concept & Execution Trace Test Key
len(code)is7;code[7]raisesIndexErrorbecause valid positive indices are0through6.s[0:3]returns"EPI"(length \(3 - 0 = 3\));s[-4:]returns"LOGY".- Prints
' dhaka '! Because strings are immutable,city.strip().upper()created a new string and immediately discarded it since it was never reassigned (city = ...). - Fails because
strobjects are immutable; Python raisesTypeError('str' object does not support item assignment). serial_strpreserves leading zeros ("0042",str);int("0042")drops leading zeros and evaluates to integer42(int).- Produces
["148", "92"]—a list of strings (str), not integers! Always cast withint(...)before comparing blood pressures numerically. f"{rate:.1%}"produces"4.5%". Writingf"{rate * 100:.1%}"multiplies by \(100\) twice and wrongly prints"450.0%"!- Evaluates to
"PAT-00019"(:05dpads19with three leading zeros to reach an exact total width of 5 digits). - Always returns
<class 'str'>—input()never automatically converts text tofloatorint. raw=" malaria_pos "\(\rightarrow\)clean="MALARIA_POS"\(\rightarrow\)result="MALARIA-POS".
3.8.2 B. Annotated Solutions — Guided To-Dos (Mirrored in 02_guided_todos_SOLUTION.ipynb)
# To-Do 3.1
raw_district = " sylhet_sadar "
clean_district = raw_district.strip().replace("_", " ").title()
char_count = len(clean_district) # 12
# To-Do 3.2
patient_id = "BD-DHK-2026-0482"
country_code = patient_id[:2] # "BD"
district_code = patient_id[3:6] # "DHK"
cohort_year = int(patient_id[7:11]) # 2026
serial_str = patient_id[-4:] # "0482"
# To-Do 3.3
raw_record = " PAT_109 | 38.4 C | dengue_ns1_pos "
parts = raw_record.split("|")
lab_id = parts[0].strip() # "PAT_109"
temp_c = float(parts[1].replace("C", "").strip()) # 38.4
is_dengue_pos = "pos" in parts[2].strip().lower() # True
# To-Do 3.4
raw_code = "uhc_0042"
fixed_code = raw_code.upper().replace("_", "-") # "UHC-0042"
replaced_facility = "DHK" + fixed_code[3:] # "DHK-0042"
# To-Do 3.5
district, screened, positive = "Rajshahi", 12450, 386
prevalence = positive / screened
mean_hb = 11.246
report_line = (
f"{district}: n={screened:,} | Pos={positive} "
f"({prevalence:.2%}) | Mean Hb={mean_hb:.1f} g/dL"
)
# To-Do 3.6
raw_name, raw_age = " fatema akter ", " 28 "
raw_seq, raw_temp = " 7 ", " 38.6 C "
clean_name = raw_name.strip().title() # "Fatema Akter"
age_int = int(raw_age.strip()) # 28
temp_c = float(raw_temp.replace("C", "").strip()) # 38.6
case_tag = f"CASE-{int(raw_seq.strip()):04d}" # "CASE-0007"3.8.3 C. Annotated Solutions — Chapter 3 Coding Exam (Mirrored in 03_coding_exam_SOLUTION.ipynb)
# Exam Task 3.1 (30 pts)
raw_barcode = " bd-ctg-2026-0094-mal "
clean_barcode = raw_barcode.strip().upper() # "BD-CTG-2026-0094-MAL"
tokens = clean_barcode.split("-")
division_code = tokens[1] # "CTG"
sample_year = int(tokens[2]) # 2026
sample_number = int(tokens[3]) # 94
is_malaria_panel = clean_barcode.endswith("MAL") # True
# Exam Task 3.2 (35 pts)
raw_payload = (
" rahim_uddin ; 45 yrs ; 72.4 kg ; 168.0 cm ; 148/92 mmHg "
)
fields = raw_payload.split(";")
patient_name = fields[0].strip().replace("_", " ").title()
age_years = int(fields[1].replace("yrs", "").strip()) # 45
weight_kg = float(fields[2].replace("kg", "").strip()) # 72.4
height_m = float(fields[3].replace("cm", "").strip()) / 100
bmi = round(weight_kg / (height_m ** 2), 2) # 25.65
bp_parts = fields[4].replace("mmHg", "").strip().split("/")
sbp_mmhg, dbp_mmhg = int(bp_parts[0]), int(bp_parts[1])
# Exam Task 3.3 (35 pts)
facility_code, report_seq = "uhc-syl-03", 14
population, confirmed_cases, deaths = 340500, 512, 9
report_id = f"{facility_code.upper()}-R{report_seq:04d}"
incidence_per_100k = round((confirmed_cases / population) * 100000, 2)
cfr_pct_str = f"{deaths / confirmed_cases:.2%}" # "1.76%"
bulletin_line = (
f"[{report_id}] Pop={population:,} | "
f"Cases={confirmed_cases:,} ({incidence_per_100k:.1f}/100k) | "
f"CFR={cfr_pct_str}"
)3.8.4 D. Debugging Post-Mortem: Top 3 Chapter 3 Failure Modes
- The Silent Immutability Trap (
raw.strip()Without Assignment): Callingraw_district.strip()looks like it should cleanraw_district, and Python produces zero error message—yetraw_districtstays completely unchanged! Always bind the returned copy (clean_district = raw_district.strip()). - Double-Multiplying Percentages (
{rate * 100:.2%}): Because the%format specifier automatically multiplies proportions by \(100\), multiplying by100beforehand turns a1.76%Case Fatality Rate into an impossible175.78%! Always pass the raw decimal proportion (deaths / confirmed_cases) directly into:.2%. - Hidden Spaces Breaking
.endswith()and Exact Matching: Checkingraw_barcode.upper().endswith("MAL")before calling.strip()silently returnsFalsebecause the last two characters are spaces (" "), not"MAL"! Make.strip()the very first link in every text-cleaning chain.


