>>> a = "545.2222"
>>> float(a)
545.22220000000004
>>> int(float(a))
545
Answer from Harley Holcombe on Stack Overflow
Top answer
1 of 16
3127
>>> 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:

def 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.

Discussions

i am a beginner at python and im trying to convert string to int but it doesn't work
What? You can't convert "sadsad" to an int. What would that even mean? Don't you mean you want to convert the integer 170 to a string, instead? More on reddit.com
🌐 r/learnpython
21
1
August 8, 2023
how to use int() and str() properly
Use int to parse strings to integers like int("123") == 123. Or convert stuff like floating point numbers to integers: int(123.4) == 123. Use str to obtain string representations of various objects like str(123) == "123", str([1,2,3]) == "[1, 2, 3]" and so on. More on reddit.com
🌐 r/learnpython
17
0
February 16, 2024
How does computer converts integer to string?
It somewhat depends on the character set used. For ASCII, it's literally just a big lookup table that maps number to character. For unicode it's a bit more complex but on a basic level it's still similar. More on reddit.com
🌐 r/learnprogramming
16
82
March 29, 2022
Converting a string to an int when the string contains numbers and letters
If the format is consistent, just split by whitespace and pick the first element: int(s.split()[0]) More on reddit.com
🌐 r/learnpython
32
6
November 8, 2023
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-string-to-int-int-to-string
Python String to Int, Int to String | DigitalOcean
Technical tutorials, Q&A, events — This is an inclusive place where developers can find or lend support and discover new ways to contribute to the community.
🌐
Career Karma
careerkarma.com › blog › python › python string to int() and int to string tutorial: type conversion in python
Python String to Int() and Int to String Tutorial: Type Conversion in Python
December 1, 2023 - Use the syntax print(str(INT)) to return the int as a str, or string. When learning how to code with Python, students quickly find that Python includes a number of data types that are used to distinguish a particular type of data.
🌐
GeeksforGeeks
geeksforgeeks.org › python › convert-string-to-integer-in-python
Convert String to Int in Python - GeeksforGeeks
Explanation: s.isdigit() returns True if s contains only digits, this allow us safe conversion to an integer. Note: isdigit() returns False for negative numbers (such as "-12") and decimal values (such as "3.14"), so it is suitable only for strings containing digits only.
Published: June 15, 2026
🌐
Real Python
realpython.com › convert-python-string-to-int
How to Convert a Python String to int – Real Python
March 18, 2026 - It’s important to recognize the difference between two types of failed results of passing a string to int(): Syntax Error: A ValueError will occur when int() doesn’t know how to parse the string using the provided base (10 by default).
Find elsewhere
🌐
SheCodes
shecodes.io › athena › 2142-converting-an-integer-to-string-in-python
[Python] - Converting an Integer to String in Python - | SheCodes
Learn how to convert an integer to a string in Python by using the `str()` function or casting the integer as a string.
🌐
GeeksforGeeks
geeksforgeeks.org › convert-integer-to-string-in-python
Convert integer to string in Python - GeeksforGeeks
While it also converts integers to strings, it’s not the most common method for simple integer-to-string conversion. ... Explanation: repr(n) returns a string '42'. It’s similar to str() but usually used for debugging. ... Hexadecimal representation is commonly used in computer science and programming, especially when dealing with low-level operations or data encoding. In Python, converting a hex string to an integer is a frequent operation, and developers have multiple approaches at their disposal to achieve this tas
Published: April 25, 2025
🌐
FavTutor
favtutor.com › blogs › int-to-string-python
Convert Int to String in Python (6 Ways) | FavTutor
July 18, 2026 - In Python, the standard way to convert an integer to a string is the built-in str() function.
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-convert-integers-to-strings-in-python-3
How To Convert Integers to Strings in Python 3 | DigitalOcean
September 3, 2020 - We can convert numbers to strings using the str() method. In this quick tutorial, you’ll learn how to use this method to convert integers to strings.
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-convert-data-types-in-python-3
How To Convert Data Types in Python 3 | DigitalOcean
This Python 3 tutorial will guide you through converting data types including numbers, strings, tuples and lists, as well as provide examples to help familia…
🌐
Scaler
scaler.com › home › topics › how to convert int to string in python
How to Convert int to string in Python - Scaler Topics
May 12, 2024 - ... The syntax for using the %s keyword is enclosing %s in quotation marks and then writing % integer after it. ... The syntax for using the .format() function in int to string conversion is using it with an empty string and then writing the integer value inside parenthesis.
🌐
W3Schools
w3schools.in › python › examples › convert-int-to-string
Python Program to Convert Int to String - W3schools
Using f-strings feature (available in Python 3.6 and above). Using % operator (also known as the "string formatting operator"). To convert an integer to a string in Python, the str() function can be used. This function takes an integer as an argument and returns the corresponding string ...
🌐
Mimo
mimo.org › tutorials › python › how-to-convert-integer-to-string-in-python
How to Convert Integer to String in Python
What to look for: f-strings work in Python 3.6+. If you’re on an older version, use format(). If you need to combine an integer with other strings, convert first or use formatting. ... For lists of integers, convert each item before joining. ... What to look for: join() only accepts strings, so map(str, nums) prevents TypeError. ... JSON requires strings when you want the value stored as text. ... You often parse strings into ints, then format them back to strings.
🌐
freeCodeCamp
freecodecamp.org › news › python-string-to-int-how-to-convert-a-string-to-an-integer-in-python
Python String to Int: How to Convert a String to an Integer in Python
January 5, 2020 - On first iteration when the variable i=1,then the variable [result=result+str(i)+“(space character)”],str(i) converts the ‘i’ which is an integer value to a string value.
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-convert-strings-to-integers-in-python-3
How To Convert Strings to Integers in Python 3 | DigitalOcean
Strings can be converted to integers by using the int() method. If your string does not have decimal places, you’ll most likely want to convert it to an inte…
🌐
Unstop
unstop.com › home › blog › convert int to string in python (6 methods with examples)
Convert Int To String In Python (6 Methods With Examples)
April 11, 2024 - One common approach to convert int to string in Python is to use built-in functions like str(), which directly converts an integer to its string representation. For instance, str(123) returns the string '123'.