How do I parse a string to a float or int in Python?
How can I convert a string to a float?
Example: “545.2222” → 545.2222
How can I convert a string to an int?
Example: “31” → 31
To convert a string to a float or int in Python, you can use the built-in float() and int() functions. For converting “545.2222” to a float, simply use float("545.2222"), and for converting “31” to an int, use int("31"). These functions handle basic string-to-number conversions, though you should be aware of potential ValueError exceptions if the string contains invalid numerical characters.
Contents
- Basic String to Float Conversion
- Basic String to Int Conversion
- Error Handling for Invalid Strings
- Advanced Parsing Techniques
- Parsing Numeric Strings with Special Formats
- Performance Considerations
- Best Practices for String Conversion
Basic String to Float Conversion
The most straightforward way to convert a string to a float in Python is using the built-in float() function. This function takes a string as input and attempts to convert it to a floating-point number.
# Basic float conversion
float_string = "545.2222"
number = float(float_string)
print(number) # Output: 545.2222
# Works with scientific notation
scientific_string = "1.23e-4"
scientific_float = float(scientific_string)
print(scientific_float) # Output: 0.000123
The float() function can handle various string representations including:
- Decimal numbers (e.g., “3.14159”)
- Scientific notation (e.g., “2.5e3”, “1.2E-5”)
- Positive and negative numbers (e.g., “-42.5”, “+3.14”)
Basic String to Int Conversion
Similarly, converting a string to an integer is done using the int() function. This function converts a string to a whole number.
# Basic int conversion
int_string = "31"
number = int(int_string)
print(number) # Output: 31
# Works with negative numbers
negative_string = "-15"
negative_int = int(negative_string)
print(negative_int) # Output: -15
# Works with positive numbers with explicit sign
positive_string = "+42"
positive_int = int(positive_string)
print(positive_int) # Output: 42
The int() function supports:
- Base 10 numbers (default)
- Other bases when specified (e.g., binary, hexadecimal, octal)
Error Handling for Invalid Strings
When converting strings that don’t represent valid numbers, both float() and int() will raise a ValueError. It’s important to handle these exceptions in your code.
# Handling ValueError for float conversion
try:
invalid_float = float("abc")
except ValueError:
print("Cannot convert 'abc' to float")
# Handling ValueError for int conversion
try:
invalid_int = int("12.34")
except ValueError:
print("Cannot convert '12.34' to int - contains decimal point")
# Using try-except blocks safely
def safe_float_conversion(s):
try:
return float(s)
except ValueError:
return None
result = safe_float_conversion("invalid")
print(result) # Output: None
Common causes of conversion errors include:
- Non-numeric characters (e.g., “hello”)
- Strings with decimal points when converting to int
- Empty strings
- Strings with leading/trailing whitespace (unless stripped first)
Advanced Parsing Techniques
For more complex scenarios, you might need advanced parsing techniques:
# Converting from strings with commas and currency symbols
import re
def parse_currency(currency_string):
# Remove currency symbols and commas
cleaned = re.sub(r'[$,€£¥]', '', currency_string)
return float(cleaned)
price = parse_currency("$1,234.56")
print(price) # Output: 1234.56
# Parsing mixed numbers and units
def parse_dimension(dimension_string):
import re
match = re.match(r'(\d+(?:\.\d+)?)([a-zA-Z]+)', dimension_string)
if match:
value = float(match.group(1))
unit = match.group(2)
return value, unit
return None, None
length, unit = parse_dimension("12.5cm")
print(f"{length} {unit}") # Output: 12.5 cm
Parsing Numeric Strings with Special Formats
Python can handle various numeric string formats:
# Different number formats
numbers = [
"12345", # Standard decimal
"-123.45", # Negative decimal
"+1.23e4", # Scientific notation
"0x1F", # Hexadecimal (requires int() with base)
"0o755", # Octal (requires int() with base)
"0b1010", # Binary (requires int() with base)
]
for num_str in numbers:
try:
if num_str.startswith(('0x', '0o', '0b')):
# Convert with base parameter
if num_str.startswith('0x'):
value = int(num_str, 16)
elif num_str.startswith('0o'):
value = int(num_str, 8)
else: # 0b
value = int(num_str, 2)
else:
# Regular decimal or scientific notation
value = float(num_str)
print(f"'{num_str}' → {value} ({type(value)})")
except ValueError as e:
print(f"Error converting '{num_str}': {e}")
Performance Considerations
When dealing with large-scale string conversions, performance matters:
# Performance comparison
import timeit
def performance_test():
# Method 1: Direct conversion
def direct_conversion():
numbers = ["123", "456", "789"] * 1000
return [float(n) for n in numbers]
# Method 2: With validation
def safe_conversion():
numbers = ["123", "456", "789"] * 1000
result = []
for n in numbers:
try:
result.append(float(n))
except ValueError:
result.append(0.0)
return result
# Method 3: Pre-compiled regex for validation
import re
number_pattern = re.compile(r'^-?\d+\.?\d*$')
def regex_conversion():
numbers = ["123", "456", "789"] * 1000
result = []
for n in numbers:
if number_pattern.match(n):
result.append(float(n))
else:
result.append(0.0)
return result
print("Direct conversion:", timeit.timeit(direct_conversion, number=100))
print("Safe conversion:", timeit.timeit(safe_conversion, number=100))
print("Regex conversion:", timeit.timeit(regex_conversion, number=100))
performance_test()
For better performance:
- Use direct conversion when you know strings are valid
- Consider list comprehensions for batch operations
- Avoid regex validation if not needed for your use case
Best Practices for String Conversion
Here are comprehensive best practices for converting strings to numbers:
# Comprehensive conversion function
def convert_string_to_number(input_string, target_type='auto', default=None, strip_whitespace=True):
"""
Robust string to number conversion with multiple options.
Args:
input_string: String to convert
target_type: 'auto', 'int', or 'float'
default: Value to return on conversion failure
strip_whitespace: Whether to strip whitespace before conversion
Returns:
Converted number or default value
"""
if not input_string:
return default
# Handle whitespace
if strip_whitespace:
input_string = input_string.strip()
try:
if target_type == 'auto':
# Try int first, then float
try:
return int(input_string)
except ValueError:
return float(input_string)
elif target_type == 'int':
return int(input_string)
elif target_type == 'float':
return float(input_string)
else:
raise ValueError(f"Invalid target_type: {target_type}")
except ValueError:
return default
# Usage examples
print(convert_string_to_number("42")) # Output: 42 (int)
print(convert_string_to_number("3.14")) # Output: 3.14 (float)
print(convert_string_to_number("42", 'float')) # Output: 42.0 (float)
print(convert_string_to_number("invalid")) # Output: None (default)
print(convert_string_to_number(" 123 ")) # Output: 123 (stripped whitespace)
Additional best practices:
- Always handle potential exceptions in production code
- Consider the source of your strings - user input, files, APIs, etc.
- Be aware of locale-specific number formats if dealing with international data
- Use type hints in your function signatures for better code documentation
- Consider using data validation libraries for complex parsing scenarios
Conclusion
Python provides simple and effective methods for converting strings to numbers using float() and int() functions. The key takeaways are:
- Basic conversion: Use
float("545.2222")for decimal numbers andint("31")for integers - Error handling: Always implement try-except blocks or use validation functions to handle invalid strings
- Advanced parsing: For complex formats like currency or mixed units, use regular expressions and custom parsing logic
- Performance: Choose appropriate conversion methods based on your data quality and performance requirements
- Robustness: Implement comprehensive conversion functions with proper validation and error handling
For most use cases, the built-in float() and int() functions will suffice, but understanding how to handle edge cases and invalid inputs is crucial for building reliable applications. When working with user input or external data sources, always validate and sanitize strings before attempting numeric conversion.