How do I parse a string to a float or int in Python?

To parse a string into a float or int in Python, use the built-in float() or int() functions. Here’s how to do it safely and handle common edge cases:

1. Basic Conversion

# Convert to integer
s = "123"
num_int = int(s)  # Output: 123 (int)

# Convert to float
s = "123.45"
num_float = float(s)  # Output: 123.45 (float)

2. Handling Errors

Use a try-except block to avoid crashes for invalid inputs:

def parse_number(s):
    try:
        return int(s)
    except ValueError:
        try:
            return float(s)
        except ValueError:
            return None  # or raise an error

print(parse_number("123"))    # Output: 123 (int)
print(parse_number("123.45")) # Output: 123.45 (float)
print(parse_number("abc"))    # Output: None

3. Clean Input First

Remove unwanted characters (e.g., whitespace, commas):

s = "  1,234.56  "
cleaned = s.strip().replace(",", "")  # "1234.56"
num_float = float(cleaned)  # 1234.56

4. Check Type Before Conversion

Determine if the string represents an integer or float:

def is_integer(s):
    try:
        int(s)
        return True
    except ValueError:
        return False

def is_float(s):
    try:
        float(s)
        return True
    except ValueError:
        return False

s = "123.45"
if is_integer(s):
    num = int(s)
elif is_float(s):
    num = float(s)
else:
    num = None

5. Edge Cases

  • Scientific notation: float("1.23e4")12300.0
  • Hexadecimal: int("0x1A", 16)26
  • Binary: int("1010", 2)10
  • Infinity/NaN: float("inf")inf, float("nan")nan

6. Localization (Commas as Decimal Separators)

For strings like "12,34" (common in EU formats):

s = "12,34"
cleaned = s.replace(",", ".")
num_float = float(cleaned)  # 12.34

Key Takeaways

  • Use int() or float() directly for valid inputs.
  • Always sanitize inputs (strip whitespace, remove commas).
  • Handle errors with try-except blocks.
  • Use helper functions to check types before conversion.

Leave a Reply

Your email address will not be published. Required fields are marked *