Python
String Manipulation
Data Cleaning
Regex
Programming Tips

Removing all non-numeric characters from string in Python

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Removing non-numeric characters in Python is simple for one-off scripts, but production data cleaning needs explicit rules. Different inputs may contain currency symbols, signs, decimal separators, or country-specific formatting. The best implementation starts by defining what numeric means for your specific pipeline.

Core Sections

Choose rules before writing code

There is no universal "keep digits" rule. A phone-number cleaner usually keeps digits only. A financial cleaner may keep one decimal point and optional leading sign. Define your contract first, then implement accordingly.

Examples of valid output contracts:

  • Digits only for IDs and phone normalization.
  • Signed decimal for numeric measurement parsing.
  • Digits plus one locale-aware separator for regional amounts.

Fast regex solution for digits-only output

If you truly need digits only, regex is concise and efficient.

python
1import re
2
3raw = "Invoice #A-19: total $1,204.55"
4clean = re.sub(r"\D", "", raw)
5print(clean)  # 19120455

This removes everything except ASCII digits. It is ideal for identifiers but not for decimal values.

Preserve signs and decimal points when needed

For numeric values, implement a stricter parser instead of blindly deleting characters.

python
1import re
2
3
4def clean_signed_decimal(text: str) -> str:
5    text = text.strip()
6    text = re.sub(r"[^0-9+\-.]", "", text)
7
8    # keep sign only at start
9    text = re.sub(r"(?!^)[+-]", "", text)
10
11    # keep only first decimal point
12    if text.count('.') > 1:
13        first = text.find('.')
14        text = text[:first + 1] + text[first + 1:].replace('.', '')
15
16    return text
17
18print(clean_signed_decimal(" temp=-12.5C "))
19print(clean_signed_decimal("USD +1,234.50"))

This keeps behavior predictable and avoids hidden parsing bugs.

Handle Unicode and locale cases explicitly

D covers many digit scenarios, but localized number strings still need deliberate normalization. For example, one locale may use comma as decimal separator and period as thousands separator.

A robust pipeline should normalize locale-specific formats before numeric conversion. Do not mix locale assumptions implicitly across data sources.

Validate conversion with decimal for finance paths

After cleaning, parse with Decimal instead of float when exactness matters.

python
1from decimal import Decimal, InvalidOperation
2
3
4def parse_amount(text: str) -> Decimal:
5    cleaned = clean_signed_decimal(text)
6    if cleaned in {"", "+", "-", ".", "+.", "-."}:
7        raise ValueError("No numeric value found")
8    try:
9        return Decimal(cleaned)
10    except InvalidOperation as ex:
11        raise ValueError(f"Invalid numeric value: {text}") from ex
12
13print(parse_amount("$42.300"))

This catches malformed values early and prevents downstream arithmetic errors.

Build reusable cleaners per field type

Do not use one generic cleaner for every input field. Create dedicated functions such as clean_phone, clean_amount, and clean_postal_code, each with its own tests. This avoids accidental rule leakage where one field's logic breaks another field.

Reusable field-specific cleaners also improve readability for teammates and make review discussions concrete.

Add tests for edge and malformed input

Include tests for empty strings, multiple separators, misplaced signs, and mixed symbols. Test both valid and invalid examples so expected failure behavior is documented.

When data sources change, these tests quickly reveal whether cleaning rules still match real inputs.

Keep parsing telemetry lightweight and actionable

In high-volume pipelines, record only aggregate cleaning metrics such as invalid input count, parse failure rate, and top failure examples after redaction. This gives enough operational visibility without flooding logs with raw sensitive data.

A small daily report of parse errors can reveal upstream format changes early and lets teams update cleaning rules before downstream analytics are affected.

Common Pitfalls

  • Using digits-only cleaning when signed decimal values are required.
  • Removing separators without defining locale rules for number formats.
  • Converting to float and losing precision in financial workflows.
  • Accepting malformed cleaned strings without validation.
  • Reusing one cleaning function across unrelated data fields.

Summary

  • Define field-specific numeric contracts before implementing cleaners.
  • Use regex digits-only cleaning only for identifier-like data.
  • Preserve sign and decimal behavior explicitly for numeric values.
  • Parse critical values with Decimal and explicit validation.
  • Cover edge cases with tests so behavior stays stable as inputs evolve.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.