YouTip LogoYouTip

Python String To Int

# Python String to Integer Conversion: Implementing a Custom `atoi` Function In Python, converting a string to an integer is typically done using the built-in `int()` function. However, understanding how to implement this conversion from scratch is a classic programming exercise (often referred to as `atoi` or ASCII-to-integer). This tutorial demonstrates how to implement a custom string-to-integer conversion function in Python. The custom function handles leading/trailing whitespace, positive and negative signs, and stops parsing when it encounters non-numeric characters. --- ## 1. The Custom Conversion Algorithm The custom function `str_to_int()` mimics the behavior of standard conversion utilities. It processes the input string step-by-step: 1. **Whitespace Trimming**: Removes any leading or trailing spaces. 2. **Sign Detection**: Checks if the number is positive (`+`) or negative (`-`). 3. **Character-by-Character Parsing**: Iterates through the numeric characters and builds the integer using ASCII values (`ord()`), stopping immediately if a non-digit character is encountered. --- ## 2. Code Implementation Here is the complete Python implementation: ```python def str_to_int(s: str) -> int: # Step 1: Remove leading and trailing whitespace s = s.strip() if not s: return 0 # Step 2: Initialize sign and check for '+' or '-' sign = 1 if s == '-' or s == '+': if s == '-': sign = -1 s = s[1:] # Remove the sign character from the string # Step 3: Convert numeric characters to an integer result = 0 for char in s: if not char.isdigit(): break # Stop conversion if a non-digit character is found # Convert character to digit using ASCII offset and accumulate result = result * 10 + (ord(char) - ord('0')) return sign * result # --- Test Cases --- print(str_to_int(" 123")) # Output: 123 print(str_to_int(" -456")) # Output: -456 print(str_to_int(" +789")) # Output: 789 print(str_to_int(" 12a34")) # Output: 12 (stops parsing at 'a') ``` ### Output ```text 123 -456 789 12 ``` --- ## 3. Detailed Code Explanation * **`s.strip()`**: This built-in method cleans up the input by removing any leading or trailing spaces, ensuring that inputs like `" 123 "` are processed correctly. * **`if not s:`**: If the string becomes empty after stripping (e.g., the input was just spaces `" "`), the function safely returns `0`. * **Sign Handling (`sign = 1`)**: We set a default multiplier of `1` (positive). If the first character is `'-'`, we update the multiplier to `-1`. We then slice the string (`s[1:]`) to discard the sign character and focus only on the digits. * **`char.isdigit()`**: This check ensures we only process valid numeric characters. If a non-digit character (like `'a'` in `"12a34"`) is encountered, the loop terminates early via `break`. * **`ord(char) - ord('0')`**: This is a low-level programming technique to convert a character digit to its actual integer value without using `int()`. By subtracting the ASCII value of `'0'` (48) from the ASCII value of the current character, we get the exact integer representation (e.g., `ord('5') - ord('0')` yields `53 - 48 = 5`). * **`result * 10 + ...`**: To reconstruct the multi-digit number, we shift the existing `result` one decimal place to the left (by multiplying by 10) and add the newly parsed digit. --- ## 4. Key Considerations & Edge Cases When implementing or using string-to-integer conversions, keep the following behaviors in mind: | Input Scenario | Custom `str_to_int` Behavior | Built-in `int()` Behavior | | :--- | :--- | :--- | | **Leading/Trailing Spaces** (`" 42 "`) | Returns `42` | Returns `42` | | **Signed Numbers** (`"-100"`, `"+50"`) | Returns `-100`, `50` | Returns `-100`, `50` | | **Mixed Characters** (`"12a34"`) | Returns `12` (truncates at `'a'`) | Raises `ValueError` | | **Empty / Whitespace-only String** | Returns `0` | Raises `ValueError` |
← Python List UnionPython String Prefixes β†’