With Python < 3 (e.g. 2.6 [see comments] or 2.7), there are two ways to do so.

# Option one
older_method_string = "%.9f" % numvar

# Option two
newer_method_string = "{:.9f}".format(numvar)

But note that for Python versions above 3 (e.g. 3.2 or 3.3), option two is preferred.

For more information on option two, I suggest this link on string formatting from the Python documentation.

And for more information on option one, this link will suffice and has info on the various flags.

Python 3.6 (officially released in December of 2016), added the f string literal, see more information here, which extends the str.format method (use of curly braces such that f"{numvar:.9f}" solves the original problem), that is,

# Option 3 (versions 3.6 and higher)
newest_method_string = f"{numvar:.9f}"

solves the problem. Check out @Or-Duan's answer for more info, but this method is fast.

Answer from jyalim on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › how i convert float to string in this case?
r/learnpython on Reddit: How I convert float to string in this case?
August 21, 2022 -

I tried

n1=input('First number')
n2=input('Second number')
sum = float(n1) + float(n2)
str(sum)
print('The sum of the values is: ' + sum)

My error is:

TypeError: can only concatenate str (not "float") to str

I tried googling this error and got some answers like print(f' which I didn't really understand, and some others that looked a little complicated, I am very new.

I am trying to improve my googling skills.

🌐
Reddit
reddit.com › r/learnpython › python string to float conversion
r/learnpython on Reddit: Python string to float conversion
July 29, 2024 -

Hi all, I have a string a = '1721244344.700249000', I want to convert it to a floating value.

Float() is returning only 2 places after decimal point. Like 1721244344.7

Is there a way I can convert the entire string to a floating point value and get all the decimal (upto 9 places after decimal point)?

I have to use python v2.7 for this.

Edit: I do not have problem in printing the all 9 decimal places but I need the floating value so that I can subtract another value so that I get the difference with accuracy upto 9 th decimal point.

🌐
GeeksforGeeks
geeksforgeeks.org › python › convert-string-to-float-in-python
Convert String to Float in Python - GeeksforGeeks
July 15, 2025 - float() function is an efficient way to convert a string into a floating-point number. As a built-in function, it returns a value of type float and works best with well-formatted numeric strings.
🌐
Python
docs.python.org › 3 › builtins › stdtypes.html
Built-in Types — Python 3.14.7 documentation
Return a representation of a floating-point number as a hexadecimal string. For finite floating-point numbers, this representation will always include a leading 0x and a trailing p and exponent. ... Class method to return the float represented by a hexadecimal string s.
🌐
IONOS
ionos.com › digital guide › websites › web development › converting python strings to floats
How to convert Python strings to floats - IONOS
February 1, 2025 - In programs that work with various kinds of data, float() is used to ensure that data remains con­sis­tent by con­vert­ing values into a numerical format. To convert Python strings to floats, use float() and enter a valid string as the argument:
Find elsewhere
🌐
Python
docs.python.org › 3 › library › string.html
string — Common string operations
This option is only valid for integer, float and complex types. For integers, when binary, octal, or hexadecimal output is used, this option adds the respective prefix '0b', '0o', '0x', or '0X' to the output value. For float and complex the alternate form causes the result of the conversion to always contain a decimal-point character, even if no digits follow it.
🌐
Sentry
sentry.io › sentry answers › python › how do i parse a string to a float or int?
Python: How to Parse a String to a Float or Int | Sentry
January 30, 2023 - try: not_a_float = float("123.456.789") except ValueError: print("Could not parse string to float") ... Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski. SEE EPISODES ... James W. — October 21, 2022 ... David Y. — May 15, 2023 ... David Y. — March 15, 2023 · Get the current script file's full directory path in Python
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-convert-string-to-float
How to Convert String to Float in Python: Complete Guide with Examples | DigitalOcean
Learn how to convert strings to floats in Python using float(). Includes syntax, examples, error handling tips, and real-world use cases for data parsing.
🌐
Real Python
realpython.com › how-to-python-f-string-format-float
How to Format Floats Within F-Strings in Python – Real Python
March 18, 2026 - In older versions of Python, f-strings had a number of other limitations that were only fixed with Python version 3.12. This version is used throughout this tutorial. Take a look at the example below. It shows you how to embed the result of a calculation within an f-string: ... >>> f"One third, expressed as a float is: {1 / 3}" 'One third, expressed as a float is: 0.3333333333333333'
🌐
W3Schools
w3schools.com › python › python_casting.asp
Python Casting
Python is an object-orientated language, and as such it uses classes to define data types, including its primitive types. Casting in python is therefore done using constructor functions: int() - constructs an integer number from an integer literal, a float literal (by removing all decimals), or a string literal (providing the string represents a whole number)
🌐
W3Schools
w3schools.com › python › ref_func_float.asp
Python float() Function
Remove List Duplicates Reverse ... Study Plan Python Interview Q&A Python Training ... The float() function converts the specified value into a floating point number. ... Coding fundamentals as bite-sized lessons and challenges. ...
🌐
Python.org
discuss.python.org › python help
Is it legal/valid to use float() to convert a string containing integer to float? - Python Help - Discussions on Python.org
December 22, 2022 - Hi, I’m wondering if float(‘10’) (or any integer string) can be safely converted using float() to float number. Documentation of float() states that: the input must conform to the following grammar after leading and trailing whitespace characters are removed: sign ::= "+" | "-" infinity ::= "Infinity" | "inf" nan ::= "nan" numeric_value ::= floatnumber | infinity | nan numeric_string ::= [sign] numeric_value where floatnumber, described in Floating point li...
🌐
Mimo
mimo.org › tutorials › python › how-to-convert-string-to-float-in-python
How to Convert String to Float in Python
Use float("3.14") to convert a numeric string to a float. Clean the string first for spaces and separators, then convert. Wrap conversion in try/except ValueError for messy input. Choose one comma strategy, thousands separators and comma decimals differ. Check for empty or whitespace-only strings ...
🌐
Educative
educative.io › answers › how-to-convert-float-to-string-in-python
How to convert float to string in Python
On the other hand, in languages like JavaScript or Python, there is no need to explicitly mention a variable’s datatype at the time of declaration as the datatype is set according to the value assigned to it. #JavaScript var my_int = 123 var my_float = 123.123 var my_string = "123" #Python my_int = 123 my_float = 123.123 my_string = "123"
🌐
Quora
quora.com › How-can-I-convert-a-float-to-a-string-in-Python
How to convert a float to a string in Python - Quora
Answer (1 of 7): # First take any variable and assign any value to it float = 1.2 # Next take another variable(result in this code) # and use python str keyword to convert float value # to str and assign that value to another variable(result in this code) result = str(float) # print the seco...
Top answer
1 of 1
1
data-user_answers_id="5008">How To Convert a String to a Float in PythonIn this article, we'll explore how to convert strings to numbers using Python's float() function. Additionally, we'll learn how to convert numbers back to strings using Python's str() function.It's crucial to convert data types correctly before using them for calculations and combining them to avoid unexpected errors while the program is running.Using the float() function:In Python, you can change a string into a floating-point number using the float() function. This built-in function is designed to convert an object into a floating-point number. Essentially, the float() function invokes the specified object's float() function internally.ExampleLet's check out a simple example of converting a string to a float in Python:string_value = '9.56'float_value = float(string_value)print(type(float_value))print('Float Value =', float_value)OutputFloat Value = 9.56In this example, the float() function is used to convert the string "9.56" to a floating-point number. The resulting value is stored in the variable float_number.Why do we need to convert a string to float?When users provide float values through the terminal or when reading from a file, these values are initially in the form of string objects. To carry out operations like addition, multiplication, and more, it's important to convert these strings into float so that we can perform the required mathematical operations accurately.Examplevalue_1 = input('Please enter the first floating-point value:\n')value_1 = float(value_1)value_2 = input('Please enter the second floating-point value:\n')value_2 = float(value_2)print(f'Sum of {value_1} and {value_2} is {value_1 + value_2}')OutputPlease enter the first floating-point value:23.34Please enter the second floating-point value:365.34Sum of 23.34 and 365.34 is 388.6799The sum of 23.34 and 365.34 is calculated to be 388.6799.Using the str() function:We can also change a float to a string using the str() function. This becomes useful when we need to combine or concatenate float values with other strings.Example:value_1 = 1.23value_2 = 2.34value_3 = 3.45print(f'Concatenation of {value_1} and {value_2} is {str(value_1) + str(value_2)}')print(f'CSV from {value_1}, {value_2}, and {value_3}:\n{str(value_1)},{str(value_2)},{str(value_3)}')print(f'CSV from {value_1}, {value_2}, and {value_3}:\n{", ".join()}')OutputConcatenation of 1.23 and 2.34 is 1.232.34CSV from 1.23, 2.34, and 3.45:1.23,2.34,3.45CSV from 1.23, 2.34, and 3.45:1.23, 2.34, 3.45When we concatenate 1.23 and 2.34, the result is the string '1.232.34'. Additionally, this code generates two versions of comma-separated values (CSV).In the above program, if we skip converting floats to strings, the join() function will cause an exception. Additionally, using the + operator for concatenation won't work correctly, as it would attempt to add the floating-point numbers instead.Conclusion:In this tutorial, we've covered the conversion between strings and numbers in Python using the float() and str() functions. Ensuring proper data type conversion is crucial for accurate and error-free operations in a program.We started by exploring the float() function, which is used to convert strings to floating-point numbers. This is particularly important when handling user input or reading data from files where numeric values are initially in string form. We demonstrated its usage through examples, highlighting the significance of this conversion for mathematical operations.
🌐
Facebook
facebook.com › groups › selftaughtprogrammers › posts › 1759043631126030
How do i convert string into float in python?
Popular groups · Find communities for you · Over 1 billion people across the globe are using Facebook Groups to explore their favorite topics · Log in · Categories · Science & tech · Travel · Animals · Sports & fitness · Entertainment