🌐
GeeksforGeeks
geeksforgeeks.org › dsa › write-your-own-atoi
String to Integer - Write your own atoi() - GeeksforGeeks
Write your own atoi() that takes a string (which represents a floating point value) as an argument and returns its value as double.
Published: April 14, 2026
🌐
AlgoMonster
algo.monster › home › 8. string to integer (atoi)
8. String to Integer (atoi) - In-Depth Explanation
Handle overflow: The result must be within the range of a 32-bit signed integer [-2^31, 2^31 - 1], which is [-2147483648, 2147483647]. If the number would overflow: ... The solution needs to carefully handle edge cases like empty strings, strings with only spaces, and integer overflow situations during the conversion process.
Discussions

Python's equivalent of C's atoi
s = '21st century' num = int(s.replace(s.lstrip('0123456789'), '')) The way this works: s.lstrip('0123456789') will strip all digits from the beginning of the string, so it will return a new string 'st century'. Then, s.replace('st century','') is going to replace 'st century' in s with an empty string '', so that only '21' remains. Finally, '21' is converted to int. More on reddit.com
🌐 r/learnpython
9
2
December 11, 2018
python - LeetCode 8: String to Integer (atoi) - Code Review Stack Exchange
I'm posting a solution for LeetCode's "String to Integer (atoi)". If you'd like to review, please do. Thank you! Problem Implement atoi which converts a string to an integer. The function More on codereview.stackexchange.com
🌐 codereview.stackexchange.com
November 13, 2020
Python equivalent to atoi / atof - Stack Overflow
Python loves raising exceptions, which is usually great. But I'm facing some strings I desperately want to convert to integers using C's atoi / atof semantics - e.g. atoi of "3 of 12", "3/12", "3 /... More on stackoverflow.com
🌐 stackoverflow.com
python - How do I parse a string to a float or int? - Stack Overflow
How can I convert an str to a float? "545.2222" -> 545.2222 Or an str to a int? "31" -> 31 For the reverse, see Convert integer to string in Python and Converting a float... More on stackoverflow.com
🌐 stackoverflow.com
🌐
GitHub
github.com › ChenglongChen › LeetCode-3 › blob › master › Python › string-to-integer-atoi.py
LeetCode-3/Python/string-to-integer-atoi.py at master · ChenglongChen/LeetCode-3 · GitHub
:pencil: Python / C++ 11 Solutions of All 468 LeetCode Questions - LeetCode-3/Python/string-to-integer-atoi.py at master · ChenglongChen/LeetCode-3
Author: ChenglongChen
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › python-program-to-write-your-own-atoi
Python Program To Write Your Own atoi() - GeeksforGeeks
July 23, 2025 - Return Value: If strn is a valid input, then the function returns the equivalent integer number for the passed string number. If no valid conversion takes place, then the function returns zero. Now let's understand various ways in which one can create their own atoi() function supported by various conditions:
🌐
LeetCode
leetcode.com › problems › string-to-integer-atoi
String to Integer (atoi) - LeetCode
8. String to Integer (atoi) Medium · Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer. The algorithm for myAtoi(string s) is as follows: Whitespace: Ignore any leading whitespace (" "). Signedness: ...
🌐
CodeSpeedy
codespeedy.com › home › string.atoi in python
string.atoi in Python - CodeSpeedy
February 8, 2020 - Following is a code if you want ... atoi to convert the string to type int. The ord() method returns an integer representing Unicode point for the given Unicode character....
🌐
Beautiful Soup
tedboy.github.io › python_stdlib › generated › generated › string.atoi.html
string.atoi() — Python Standard Library
string.atoi(s[, base]) → int[source]¶ · Return the integer represented by the string s in the given base, which defaults to 10. The string s must consist of one or more digits, possibly preceded by a sign. If base is 0, it is chosen from the leading characters of s, 0 for octal, 0x or 0X ...
Find elsewhere
🌐
Educative
educative.io › home › courses › decode the coding interview in python: real-world examples › diy: string to integer (atoi)
Convert String to Integer in Python Using atoi Method
Learn to convert strings to 32-bit signed integers in Python by handling whitespaces, signs, and invalid inputs with the atoi function.
🌐
HeyCoach Blog
heycoach.in › blog › string-to-integer-atoi-solution-in-python
String To Integer (atoi) Solution In Python
June 12, 2025 - Extract Digits: Loop through the string, converting characters to digits until a non-digit is encountered. Handle Overflow: Ensure the result stays within the 32-bit signed integer range.
🌐
YouTube
youtube.com › watch
STRING TO INTEGER (ATOI) | PYTHON | LEETCODE # 8 - YouTube
In this video we are solving a popular interview question at top tech companies Google, Facebook, Microsoft, and others. This is a pretty simple question but...
Published: March 2, 2022
🌐
Red Quark
redquark.org › leetcode › 0008-string-to-integer-atoi
LeetCode #8 - String To Integer (atoi) | Red Quark
The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.
🌐
GitHub
github.com › doocs › leetcode › blob › main › solution › 0000-0099 › 0008.String to Integer (atoi) › README_EN.md
leetcode/solution/0000-0099/0008.String to Integer (atoi)/README_EN.md at main · doocs/leetcode · GitHub
Conversion: Read the integer by skipping leading zeros until a non-digit character is encountered or the end of the string is reached. If no digits were read, then the result is 0. Rounding: If the integer is out of the 32-bit signed integer ...
Author: doocs
🌐
statyang
statyang.wordpress.com › python-practice-18-string-to-integer-atoi
Python practice 18: String to Integer (atoi) – statyang
February 19, 2015 - class Solution: # @return an integer def atoi(self, str): if str==None or str=='': return 0 rs=str.strip() tempi=0 for i in range(len(rs)): tempi=i if i==0 and rs[i]=='+':continue if i==0 and rs[i]=='-':continue if i==1 and (rs[0]=='+' or rs[0]=='-') and (rs[i]>'9' or rs[i]<'0'): return 0 if rs[i]>'9' or rs[i]<'0': tempi=i-1 break rs=rs[:(tempi+1)] if rs=='': return 0 if int(rs)>2**31-1: return 2**31-1 elif int(rs)<(-2**31): return -2**31 else: return int(rs)
🌐
TutorialsPoint
tutorialspoint.com › python-program-to-write-your-own-atoi
Python Program To Write Your Own atoi()
July 11, 2023 - Explanation: The string represents a valid number, so we get the same output as an integer. ... Explanation: The string contains whitespaces and characters, making it invalid. ... In this approach, we assume the string is valid and contains ...
🌐
LeetCode
leetcode.com › problems › string-to-integer-atoi › solutions › 3232205 › python-solution-for-string-to-integer-atoi-problem
Python solution for String to Integer (atoi) problem
February 26, 2023 - Can you solve this real interview question? String to Integer (atoi) - Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer. The algorithm for myAtoi(string s) is as follows: 1. Whitespace: Ignore any leading ...
🌐
O'Reilly
oreilly.com › library › view › python-programming-with › 0201616165 › 0201616165_ch10lev1sec1.html
Conversion: atoi(), atof(), atol() - Python Programming with the Java™ Class Libraries: A Tutorial for Building Web and Enterprise Applications with Jython [Book]
June 28, 2002 - Content preview from Python Programming with the Java™ Class Libraries: A Tutorial for Building Web and Enterprise Applications with Jython · atoi(s[,base]) converts a string into an integer.
Author: Richard Hightower
Published: 2002
Pages: 640
Top answer
1 of 16
3127
Use with AI>>> a = "545.2222"
>>> float(a)
545.22220000000004
>>> int(float(a))
545
2 of 16
606

Python2 method to check if a string is a float:

Use with AIdef is_float(value):
  if value is None:
      return False
  try:
      float(value)
      return True
  except:
      return False

For the Python3 version of is_float see: Checking if a string can be converted to float in Python

A longer and more accurate name for this function could be: is_convertible_to_float(value)

What is, and is not a float in Python may surprise you:

The below unit tests were done using python2. Check it that Python3 has different behavior for what strings are convertable to float. One confounding difference is that any number of interior underscores are now allowed: (float("1_3.4") == float(13.4)) is True

val                   is_float(val) Note
--------------------  ----------   --------------------------------
""                    False        Blank string
"127"                 True         Passed string
True                  True         Pure sweet Truth
"True"                False        Vile contemptible lie
False                 True         So false it becomes true
"123.456"             True         Decimal
"      -127    "      True         Spaces trimmed
"\t\n12\r\n"          True         whitespace ignored
"NaN"                 True         Not a number
"NaNanananaBATMAN"    False        I am Batman
"-iNF"                True         Negative infinity
"123.E4"              True         Exponential notation
".1"                  True         mantissa only
"1_2_3.4"             False        Underscores not allowed
"12 34"               False        Spaces not allowed on interior
"1,234"               False        Commas gtfo
u'\x30'               True         Unicode is fine.
"NULL"                False        Null is not special
0x3fade               True         Hexadecimal
"6e7777777777777"     True         Shrunk to infinity
"1.797693e+308"       True         This is max value
"infinity"            True         Same as inf
"infinityandBEYOND"   False        Extra characters wreck it
"12.34.56"            False        Only one dot allowed
u'四'                 False        Japanese '4' is not a float.
"#56"                 False        Pound sign
"56%"                 False        Percent of what?
"0E0"                 True         Exponential, move dot 0 places
0**0                  True         0___0  Exponentiation
"-5e-5"               True         Raise to a negative number
"+1e1"                True         Plus is OK with exponent
"+1e1^5"              False        Fancy exponent not interpreted
"+1e1.3"              False        No decimals in exponent
"-+1"                 False        Make up your mind
"(1)"                 False        Parenthesis is bad

You think you know what numbers are? You are not so good as you think! Not big surprise.

Don't use this code on life-critical software!

Catching broad exceptions this way, killing canaries and gobbling the exception creates a tiny chance that a valid float as string will return false. The float(...) line of code can failed for any of a thousand reasons that have nothing to do with the contents of the string. But if you're writing life-critical software in a duck-typing prototype language like Python, then you've got much larger problems.