String manipulation method
You could use the fact that leading zeros are ignored when casting to an integer. As such, you would reverse your string, make an integer cast, then cast back to a string, and reverse it again. Here's an example:
In [20]: s
Out[20]: '125647124004170000'
In [21]: s[::-1]
Out[21]: '000071400421746521'
In [22]: int(_)
Out[22]: 71400421746521
In [23]: str(_)
Out[23]: '71400421746521'
In [24]: _[::-1]
Out[24]: '12564712400417'
In lines 21 and 24, we use a complete slice with a step of -1
, which reverses the string.
As a one-liner list comprehension, you could do the following with your string s
:
[str(int(s[::-1]))[::-1] for s in strings_list]
Numerical method
A simple way to do that would be to divide the integer cast of your strings by 10 using integer division until the time integer division produces a different result from floating point division. After we're done, we convert it back to a string, if your use case so requires it. The time it takes is linear in terms of the number of zeros at the end. Worst, case, O(n)
, where n
is the length of your string.
In [7]: s = '125647124004170000'
In [8]: b = int(s)
Out[8]: 125647124004170000
(modified after @AChampion's comment)
In [9]: while b % 10 == 0:
...: b //= 10
In [10]: b
Out[10]: 12564712400417
In [11]: str(b)
Out[11]: '12564712400417'