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
🌐
Reddit
reddit.com › r/learnpython › how to eliminate trailing zeros?
r/learnpython on Reddit: How to eliminate trailing zeros?
October 3, 2019 -

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

🌐
AskPython
askpython.com › home › how to format 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.
🌐
GitHub
github.com › python › cpython › issues › 111125
Types `f` and `e` in format spec does not remove trailing zeros for floats if precision is not specified · Issue #111125 · python/cpython
October 20, 2023 - 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. Normally, a decimal-point character appears in the result of these conversions only if a digit follows it. In addition, for 'g' and 'G' conversions, trailing zeros are not removed from the result.
Author   Prometheus3375
🌐
Kodeclik
kodeclik.com › remove-trailing-zeros-in-python-string
How to remove Trailing Zeros from a given Python String
July 15, 2025 - Five ways to remove trailing zeros from a Python string. 1. Use a for loop. 2. Use string slicing operators in a while loop. 3. Use the rstrip() method. 4. Recursive removal of trailing zeros. 5. Use float() and str() functions.
🌐
Bobby Hadz
bobbyhadz.com › blog › python-remove-trailing-zeros-from-decimal
How to Remove the trailing Zeros from a Decimal in Python | bobbyhadz
If it does not, use the decimal.normalize() method to strip any trailing zeros. ... Copied!from decimal import Decimal num = Decimal('1.230000') def remove_exponent(d): return ( d.quantize(Decimal(1)) if d == d.to_integral() else d.normalize() ...
Top answer
1 of 7
30

Updated Generalized to maintain precision and handle unseen values:

import decimal
import random

def format_number(num):
    try:
        dec = decimal.Decimal(num)
    except:
        return 'bad'
    tup = dec.as_tuple()
    delta = len(tup.digits) + tup.exponent
    digits = ''.join(str(d) for d in tup.digits)
    if delta <= 0:
        zeros = abs(tup.exponent) - len(tup.digits)
        val = '0.' + ('0'*zeros) + digits
    else:
        val = digits[:delta] + ('0'*tup.exponent) + '.' + digits[delta:]
    val = val.rstrip('0')
    if val[-1] == '.':
        val = val[:-1]
    if tup.sign:
        return '-' + val
    return val

# test data
NUMS = '''
    0.0000      0
    0           0
    123.45000   123.45
    0000        0
    123.4506780 123.450678
    0.1         0.1
    0.001       0.001
    0.005000    0.005
    .1234       0.1234
    1.23e1      12.3
    -123.456    -123.456
    4.98e10     49800000000
    4.9815135   4.9815135
    4e30        4000000000000000000000000000000
    -0.0000000000004 -0.0000000000004
    -.4e-12     -0.0000000000004
    -0.11112    -0.11112
    1.3.4.5     bad
    -1.2.3      bad
'''

for num, exp in [s.split() for s in NUMS.split('\n') if s]:
    res = format_number(num)
    print res
    assert exp == res

Output:

0
0
123.45
0
123.450678
0.1
0.001
0.005
0.1234
12.3
-123.456
49800000000
4.9815135
4000000000000000000000000000000
-0.0000000000004
-0.0000000000004
-0.11112
bad
bad
2 of 7
26

You can use format strings if you want, but be aware that you might need to set your desired precision, as format strings have their own logic for this by default. Janneb suggests a precision of 17 in another answer.

'{:g}'.format(float(your_string_goes_here))

After thinking about this some more, though, I think the simplest and best solution is just to cast the string twice (as jathanism suggests):

str(float(your_string_goes_here))

Edit: Added clarification because of comment.

🌐
TestMu AI Community
community.testmuai.com › ask a question
How to format Python decimals to remove trailing zeroes? - TestMu AI Community
November 7, 2024 - How can I format a Python decimal to remove unnecessary trailing zeroes while keeping a maximum of two decimal places? For example, I want the output to look like this: 1.00 --> '1' 1.20 --> '1.2' 1.23 --> '1.23' …
🌐
Python
bugs.python.org › issue40780
Issue 40780: float.__format__() handles trailing zeros inconsistently in “general” format - Python tracker
This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/84957
Find elsewhere
🌐
McNeel Forum
discourse.mcneel.com › scripting
Modify script to remove trailing Zeros - Scripting - McNeel Forum
March 16, 2023 - I do have the following script that generates a cutting list and I need to modify so that the generated values to remove the trailing Zeros from the values. I do need 100.5 if it that is the result but the 100.0 to be rounded to 100 # -*- coding: utf8 -*- import Rhino import Rhino.Geometry ...
🌐
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 par…
🌐
Python Guides
pythonguides.com › remove-the-trailing-zeros-from-a-decimal-in-python
Remove Trailing Zeros from Decimal in Python
September 3, 2025 - Python provides this simple technique to remove trailing zeros from decimals using the float() function.
🌐
Narkive
comp.lang.python.narkive.com › KFJim7kB › need-help-removing-trailing-zeros
Need help removing trailing zeros
So really, you'd need: "{:#f}".format(float(number)).rstrip("0").rstrip(".") Which is ugly, but I guess it works. PyNoob · 2013-06-27 02:30:10 UTC · Permalink I get it now! Thank you so much for your help, I really appreciate it. :) Continue reading on narkive: Search results for 'Need help removing trailing zeros' (Questions and Answers) 14 replies ·
🌐
Sage Q&A Forum
ask.sagemath.org › question › 11051 › cutting-unnecessary-zeroes-in-float-numbers
Cutting unnecessary zeroes in float numbers - ASKSAGE: Sage Q&A Forum
Then, you can reduce the number of digits by taking x.n(digits=4) (this will also turn an integer into a floating-point number, which you may want to do for large integers). You can then apply the method str with the option no_sci=False to make sure to get a scientific notation regardless of the size of x. The method also has an option skip_zeroes.
🌐
Python
bugs.python.org › issue32790
Issue 32790: Keep trailing zeros in precision for string format option g - Python tracker
This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/76971
🌐
Python Forum
python-forum.io › thread-7788.html
Controlling trailing zeros with rounding?
I'm trying to print out floats in currency format, but no matter what numbers I specify for rounding parameters, it only prints out one 0 after the decimal point: #!/usr/bin/env python3 #FormattingStuff.py def listOfFloats(): floatsList = [20.00...
🌐
CopyProgramming
copyprogramming.com › howto › remove-trailing-zeros-after-the-decimal-point-in-python
Python: Python Code to Eliminate Trailing Zeros following Decimal Point
May 26, 2023 - Python - dropping trailing '.0' from floats, The input data is a mix of floats and strings. Desired output: 0 --> '0'. 0.0 --> '0'. 0.1 --> '0.1'. 1.0 --> '1'. I've come up with the following generator expression, but I wonder if there's a faster way: (str (i).rstrip ('.0') if i else '0' for i in lst) The truth check is there to prevent 0 from becoming an empty string. ... My intention is to establish a level of accuracy using f-strings, but I aim to exclude the trailing zeroes from appearing in the decimal section.
🌐
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 - To format floats without trailing zeros with Python, we can use the rstrip method. ... We interpolate x into a string and then call rstrip with 0 and '.' to remove trailing zeroes from the number strings.