Technical skill practice

Convert String to int in Python: Accept Counts and Reject Bad Input

To convert a string to an integer in Python, use int(text) for ordinary base-ten integer text. Invalid input raises ValueError. Keep a failed conversion distinct from a legitimate zero so that a missing or malformed count does not silently become a real measurement.

Start with a small, explicit task

Parse four fictional batch-count entries containing whitespace, zero, a decimal fraction and unrelated text. This task accepts integers only; it is not a request to round decimal quantities. Return None for rejected text so the caller can distinguish rejection from the numeric value zero.

Run the complete example

This original example was checked with Python 3.12.14. Save it as python_string_to_int.py and run it with Python 3.12. It uses only the standard library. The assertions check the stated results.

Expected output

[12, 0, None, None]

47 47

def parse_count(text):
    try:
        return int(text)
    except ValueError:
        return None
inputs = [" 12 ", "0", "3.5", "many"]
counts = [parse_count(text) for text in inputs]
assert counts == [12, 0, None, None]
assert int("2f", 16) == 47
assert int("0x2f", 0) == 47
assert parse_count("3e2") is None
print(counts)
print(int("2f", 16), int("0x2f", 0))

Explain the result

The padded 12 is accepted and becomes 12. The zero remains zero. The decimal string 3.5 fails because this conversion expects integer text, not a floating-point representation to truncate. The word many also fails. None is the sentinel selected for this function, so callers should test result is None rather than if not result, which would also reject valid zero.

The final checks state their base rules explicitly. Base 16 interprets 2f as hexadecimal; base 0 permits a recognized prefix such as 0x. That flexibility should be a deliberate input contract, not an accidental way to accept unexpected formats. The scientific-notation string 3e2 is not accepted by this base-ten integer parser.

Avoid these mistakes

Do not insert float conversion merely to make malformed integer input pass: it changes the accepted formats and truncation behavior. This helper expects strings, so it catches ValueError only. If callers can provide arbitrary object types, define that interface separately instead of masking every exception.

Questions, answered

Will int accept leading and trailing spaces?

The whitespace-padded integer in this checked example is accepted.

Should invalid input default to zero?

Only if that is an explicit rule. Here zero is meaningful, so rejected text uses a separate None result.

Sources and examples

ResumizeAI team. Examples are fictional teaching material, not customer results. Replace sample details with your own facts.

Your experience. A complete resume.

Build in Resumize with ATS-friendly templates, guided editing and a live preview. Start free, then upgrade when you need more.

Free account required. Free-plan PDF exports include a watermark.