You could use %g to achieve this:

'%g'%(3.140)

or, with Python โ‰ฅ 2.6:

'{0:g}'.format(3.140)

or, with Python โ‰ฅ 3.6:

f'{3.140:g}'

From the docs for format: g causes (among other things)

insignificant trailing zeros [to be] removed from the significand, and the decimal point is also removed if there are no remaining digits following it.

Answer from unutbu on Stack Overflow
๐ŸŒ
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 turns 24.125 into โ€œ24.125โ€ but 25.0 into โ€œ25โ€ ?
Discussions

python - How to print float to n decimal places including trailing 0s? - Stack Overflow
I need to print or convert a float number to 15 decimal place string even if the result has many trailing 0s eg: 1.6 becomes 1.6000000000000000 I tried round(6.2,15) but it returns 6.200000000000... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - dropping trailing '.0' from floats - Stack Overflow
I'm looking for a way to convert numbers to string format, dropping any redundant '.0' The input data is a mix of floats and strings. Desired output: 0 --> '0' 0.0 --> '0' 0.1 --> '0.1' 1.... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - How to format a float with a maximum number of decimal places and without extra zero padding? - Stack Overflow
I need to do some decimal place formatting in python. Preferably, the floating point value should always show at least a starting 0 and one decimal place. Example: Input: 0 Output: 0.0 Values with... More on stackoverflow.com
๐ŸŒ stackoverflow.com
decimal value in python, how to make it '0.00'? - Stack Overflow
Python is displaying 10.0 because you're displaying a floating point number, and Python will only display as many decimal places as is needed. If you want to print 10.00 for display purposes, use the string formatting operator % like so: More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ tutorial โ€บ floatingpoint.html
15. Floating-Point Arithmetic: Issues and Limitations โ€” Python 3.14.7 documentation
Since all of these decimal values share the same approximation, any one of them could be displayed while still preserving the invariant eval(repr(x)) == x. Historically, the Python prompt and built-in repr() function would choose the one with 17 significant digits, 0.10000000000000001. Starting with Python 3.1, Python (on most systems) is now able to choose the shortest of these and simply display 0.1. Note that this is in the very nature of binary floating point: this is not a bug in Python, and it is not a bug in your code either.
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ decimal.html
decimal โ€” Decimal fixed-point and floating-point arithmetic
In decimal floating point, 0.1 + 0.1 + 0.1 - 0.3 is exactly equal to zero. In binary floating point, the result is 5.5511151231257827e-017. While near to zero, the differences prevent reliable equality testing and differences can accumulate.
๐ŸŒ
w3resource
w3resource.com โ€บ python-exercises โ€บ string โ€บ python-data-type-string-exercise-32.php
Python: Print the following floating numbers with no decimal places - w3resource
June 12, 2025 - Write a Python program to print the following positive and negative numbers with no decimal places. ... # Define a variable 'x' and assign it the value 3.1415926 (a positive floating-point number). x = 3.1415926 # Define a variable 'y' and assign it the value -12.9999 (a negative floating-point number). y = -12.9999 # Print an empty line for spacing. print() # Print the original value of 'x' with a label. print("Original Number: ", x) # Format the value of 'x' with no decimal places and print it.
๐ŸŒ
AskPython
askpython.com โ€บ python โ€บ examples โ€บ formatting-floats-without-trailing-zeros
How to Format Floats Without Trailing Zeros? - AskPython
May 12, 2023 - Python offers four effective methods to remove trailing zeros from float values: the to_integral() and normalize() functions, the str() and rstrip() functions, a while loop, and the float() function.
๐ŸŒ
Medium
medium.com โ€บ @coucoucamille โ€บ float-formatting-in-python-ccb023b86417
Simple Float Formatting in Python | by Coucou Camille | Medium
June 15, 2022 - Syntax: "{:.2f}%".format(num) or "{:.2%}".format(num) to format num as a percentage with 2 decimal places. ... It adds trailing zeros to the end if the specified decimal number exceeds the original decimal place of the number:
Find elsewhere
๐ŸŒ
Finxter
blog.finxter.com โ€บ home โ€บ learn python blog โ€บ python print float without decimal points
Python Print Float Without Decimal Points - Be on the Right Side of Change
August 26, 2021 - Problem Formulation Given a floating point number x = 123.456. How to print the number without decimal remainder? Solution 1: int() Pythonโ€™s built-in function int(x) converts any float number x to an integer by truncating it towards 0. For example, positive int(1.999) will be truncated to ...
๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ python-add-zeros-after-decimal
Add zeros to a Float after the Decimal in Python | bobbyhadz
Use the `format()` function to add zeros to a float after the decimal, e.g. `result = format(my_float, '.3f')`.
๐ŸŒ
The Web Dev
thewebdev.info โ€บ home โ€บ how to format floats without trailing zeros with python?
How to format floats without trailing zeros with Python? - The Web Dev
October 23, 2021 - Therefore, n is 3.14. To format floats without trailing zeros with Python, we can use the rstrip method. ... Sometimes, we want to format a string in Python. In this article, we'll look atโ€ฆ
๐ŸŒ
Python Reference
python-reference.readthedocs.io โ€บ en โ€บ latest โ€บ docs โ€บ functions โ€บ format.html
format โ€” Python Reference (The Right Way) 0.1 documentation
The precision is a decimal number indicating how many digits should be displayed after the decimal point for a floating point value formatted with โ€˜fโ€™ and โ€˜Fโ€™, or before and after the decimal point for a floating point value formatted with โ€˜gโ€™ or โ€˜Gโ€™. For non-number types the field indicates the maximum field size - in other words, how many characters will be used from the field content.
๐ŸŒ
Luc
anh.cs.luc.edu โ€บ python โ€บ hands-on โ€บ 3.1 โ€บ handsonHtml โ€บ float.html
1.14. Decimals, Floats, and Floating Point Arithmetic โ€” Hands-on Python Tutorial for Python 3
Read the following example interpreter sequence showing possibilities when a float is being formatted: >>> x = 23.457413902458498 >>> s = format(x, '.5f') >>> s '23.45741' >>> format(x, '.2f') '23.46' >>> x 23.457413902458498 ยท Note that the results are rounded not truncated: the result to two places is 23.46, not 23.45. The formatting string '.5f' means round to 5 places after the decimal point.
๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ python-get-number-without-decimal-places
Remove the Decimal part from a Float in Python | bobbyhadz
April 9, 2024 - Use the `math.floor()` method to remove the decimal part from a float, e.g. `result = math.floor(3.999)`.
๐ŸŒ
Linux find Examples
queirozf.com โ€บ entries โ€บ python-number-formatting-examples
Python number formatting examples
August 2, 2023 - # make the total string size AT LEAST 9 (including digits and points), fill with zeros to the left '{:0>9}'.format(3.499) # >>> '00003.499'
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how to eliminate trailing zeros?
r/learnpython on Reddit: How to eliminate trailing zeros?
February 12, 2020 -

I have a float formatted to 2 decimal places. I need to eliminate the 2nd decimal place if it's a "0" but still keep 2 decimal places open for when its 2 whole numbers.

number = float(25.20458)
    print(format(number, ".2f"))
#Comes out as 25.20
#Need 25.2

Windows 10 and Python 3.7