Search code examples
pythonstrip

Need to split zeros after decimal but not left side zeros in python


Need to split zeros after decimal but not left side zeros in python

Input: 0001202406040252523670952717.00

Actual Output: 1202406040252523670952717

Expected Output: 0001202406040252523670952717

   def strip_function(num):
        return num.to_integral() if num == num.to_integral() else num.normalize()

Solution

  • You just need to rstrip any "0" then rstrip any ".":

    X = "0001202406040252523670952717.00"
    
    def strip_function(num: str) -> str:
        return num.rstrip("0").rstrip(".")
    
    print(strip_function(X))
    

    Output:

    0001202406040252523670952717