numbers = [23.23, 0.1233, 1.0, 4.223, 9887.2]                                                                                                                                                   
                                                                                                                                                                                                
for x in numbers:                                                                                                                                                                               
    print("{:10.4f}".format(x)) 

prints

   23.2300
    0.1233
    1.0000
    4.2230
 9887.2000

The format specifier inside the curly braces follows the Python format string syntax. Specifically, in this case, it consists of the following parts:

  • The empty string before the colon means "take the next provided argument to format()" โ€“ in this case the x as the only argument.
  • The 10.4f part after the colon is the format specification.
  • The f denotes fixed-point notation.
  • The 10 is the total width of the field being printed, lefted-padded by spaces.
  • The 4 is the number of digits after the decimal point.
Answer from Sven Marnach on Stack Overflow
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ string.html
string โ€” Common string operations
This limitation doesnโ€™t affect the format() function. The meaning of the various alignment options is as follows: Note that unless a minimum field width is defined, the field width will always be the same size as the data to fill it, so that the alignment option has no meaning in this case. The sign option is only valid for number types, and can be one of the following: The 'z' option coerces negative zero floating-point values to positive zero after rounding to the format precision.
Discussions

python - Convert floating point number to a certain precision, and then copy to string - Stack Overflow
I have a floating point number, say 135.12345678910. I want to concatenate that value to a string, but only want 135.123456789. With print, I can easily do this by doing something like: print "%.9... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How I convert float to string in this case?
You shouldn't call your variable sum, as that's a name of a built in function: https://docs.python.org/3/library/functions.html#sum Your problem however, stems from trying to add a string to a float. Could you please tell me, what is Adam + 5? Well, you can't, because it makes no mathematical sense. You didn't save the string representation str(sum), so sum never changed to a string What your research found is f-strings and they are very easy to use. Try: print(f"the sum of the values is {sum}") Simply, put an f before a string starts, then any string that you want goes between " and ", while any variables or other values go between { and } More on reddit.com
๐ŸŒ r/learnpython
4
2
August 21, 2022
General way to print floats without the .0 part
Iโ€™m building SVG code using data interpolation (f-strings and .format), and I have elements (the size of the graph for one) that are internally floats but which are usually integers. But when printing floats, the .0 part is always included. Is there a standard str-interpolation idiom that ... More on discuss.python.org
๐ŸŒ discuss.python.org
0
0
May 19, 2024
Python string to float conversion
I don't know what you mean: >>> a = '1721244344.700249000' >>> float(a) 1721244344.700249 These are all the decimal places. Trailing zeros will always be omitted as they are irrelevant. If you want to show them, do so when you output the value: >>> print(a.format("{:.9}")) 1721244344.700249000 More on reddit.com
๐ŸŒ r/learnpython
18
5
July 29, 2024
๐ŸŒ
EEVblog
eevblog.com โ€บ forum โ€บ programming โ€บ python-3-float-to-formated-string-to-always-fill-a-fixed-number-of-characters
Python 3 float to formatted string to always fill a fixed ...
EEVblog Captcha ยท We have seen a lot of robot like traffic coming from your IP range, please confirm you're not a robot ยท This security check has been powered by ยท CrowdSec
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_string_formatting.asp
Python String Formatting
F-String was introduced in Python 3.6, and is now the preferred way of formatting strings. Before Python 3.6 we had to use the format() method.
๐ŸŒ
Real Python
realpython.com โ€บ how-to-python-f-string-format-float
How to Format Floats Within F-Strings in Python โ€“ Real Python
April 24, 2024 - To use Pythonโ€™s format specifiers in a replacement field, you separate them from the expression with a colon (:). As you can see, your float has been rounded to two decimal places. You achieved this by adding the format specifier .2f into the replacement field. The 2 is the precision, while the lowercase f is an example of a presentation type. Youโ€™ll see more of these later. Note: When you use a format specifier, you donโ€™t actually change the underlying number. You only improve its display. Pythonโ€™s f-strings also have their own mini-language that allows you to format your output in a variety of different ways.
๐ŸŒ
Medium
medium.com โ€บ @coucoucamille โ€บ float-formatting-in-python-ccb023b86417
Simple Float Formatting in Python | by Coucou Camille | Medium
June 15, 2022 - Simple Float Formatting in Python Pythonโ€™s built-in format() function allows you to format float in any way you prefer. 1. Round Float to 2 Decimal Places Syntax: {:.2f}.format(num) for rounding to โ€ฆ
Find elsewhere
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ library โ€บ stdtypes.html
Built-in Types โ€” Python 3.14.3 documentation
1 month ago - 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.
๐ŸŒ
mkaz.blog
mkaz.blog โ€บ working-with-python โ€บ string-formatting
Python String Formatting: Complete Guide
Debugging: f"{variable=}" shows both name and value (Python 3.8+) Avoid % formatting - Legacy method, use only for compatibility ยท # Most common usage patterns name = "Alice" score = 95.67 print(f"Hello {name}! Your score is {score:.1f}%") # Output: Hello Alice! Your score is 95.7% F-strings (formatted string literals) are the modern standard for string formatting. F-strings are prefixed with f or F and allow you to embed expressions inside curly braces {}.
๐ŸŒ
PyFormat
pyformat.info
PyFormat: Using % and .format() for great good!
With this site we try to show you the most common use-cases covered by the old and new style string formatting API with practical examples. All examples on this page work out of the box with with Python 2.7, 3.2, 3.3, 3.4, and 3.5 without requiring any additional libraries.
๐ŸŒ
Python Reference
python-reference.readthedocs.io โ€บ en โ€บ latest โ€บ docs โ€บ str โ€บ formatting.html
% (String Formatting Operator) โ€” Python Reference (The Right Way) 0.1 documentation
Floating point format. Uses exponential format if exponent is less than -4 or not less than precision, decimal format otherwise. The alternate form causes the result to always contain a decimal point, and trailing zeroes are not removed as they would otherwise be. The precision determines the number of significant digits before and after the decimal point and defaults to 6. ... Single character (accepts integer or single character string...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-list-of-float-to-string-conversion
Python - List of float to string conversion - GeeksforGeeks
July 12, 2025 - List comprehension is a Pythonic way to iterate over the list and convert each element to a string. ... List comprehension iterates through the list a and applies str(i) to each element.
๐ŸŒ
CoenRaets
jsonviewer.ai โ€บ python-float-to-string
Python Float to String [Explained with Code Examples] - JSON Viewer
July 5, 2023 - Converting a float to a string in Python means converting a floating-point number to a string representation of that number. This can be done using the str() function or the format() method.
๐ŸŒ
AskPython
askpython.com โ€บ home โ€บ python floating point formatting: 2 simple methods
Python Floating Point Formatting: 2 Simple Methods - AskPython
April 10, 2025 - In this example, the :.2f inside the curly braces states that the floating-point number should be formatted with two decimal places. Running this code would output 123.46. ... Here, :10.2f states that the total width of the formatted string should be 10 characters, including two decimal places. The output, in this case, would be 123.46 ยท Pythonโ€™s format method offers a versatile approach to string formatting.
๐ŸŒ
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.

๐ŸŒ
Python.org
discuss.python.org โ€บ python help
General way to print floats without the .0 part - Python Help - Discussions on Python.org
May 19, 2024 - Iโ€™m building SVG code using data interpolation (f-strings and .format), and I have elements (the size of the graph for one) that are internally floats but which are usually integers. But when printing floats, the .0 part is always included. Is there a standard str-interpolation idiom that ...
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ python-convert-string-to-float
How to Convert String to Float in Python: Complete Guide with Examples | DigitalOcean
July 10, 2025 - API Integration: Receiving data from external APIs where numerical values are sometimes formatted as strings. Mastering this simple conversion is a key step in building robust and effective Python applications. In order to complete this tutorial, you will need: Familiarity with basic Python programming. Check out How to Code in Python 3 series or using VS Code for Python for a quick lesson. This tutorial was tested with Python 3.11.13. Python gives you a straightforward, built-in function to perform this task: float().
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
Format Strings and Numbers in Python: format() | note.nkmk.me
May 18, 2023 - Note that f-strings were introduced ... refer to the following article for more information. ... Python provides the built-in format() function for formatting strings. Built-in Functions - format() โ€” Python 3.11.3 documentation ยท This function takes the original string (str) and number (int or float) to be formatted ...
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ time.html
time โ€” Time access and conversions
Use clock_settime_ns() to avoid the precision loss caused by the float type. Availability: Unix, not Android, not iOS. Added in version 3.3. ... Similar to clock_settime() but set time with nanoseconds. Availability: Unix, not Android, not iOS. Added in version 3.7. ... Convert a time expressed in seconds since the epoch to a string of a form: 'Sun Jun 20 23:21:05 1993' representing local time.
๐ŸŒ
Mooc
programming-25.mooc.fi โ€บ part-4 โ€บ 5-print-statement-formatting
Print statement formatting - Python Programming MOOC 2025
The third method to prepare strings is f-strings. The previous example with the name and the age would look like this formulated with f-strings: name = "Erkki" age = 39 print(f"Hi {name} your age is {age} years") Thus far we have only used very simple f-strings, but they can be very versatile in formatting string type content. One very common use case is setting the number of decimals that are printed out with a floating point number.