I am trying to write a function that returns the number of trailing 0s in a string or an integer. This is what I am trying and it is not returning the correct values.
def trailing_zeros(longint): manipulandum = str(longint) x = 0 i = 1 for ch in manipulandum: if manipulandum[-i] == '0': x += x i += 1 else: return x
Maybe you can try to do it. It could be easier than counting each end "0"
def trailing_zeros(longint): manipulandum = str(longint) return len(manipulandum)-len(manipulandum.rstrip('0'))
For strings, it is probably easiest to use rstrip() :
rstrip()
In [2]: s = '23989800000' In [3]: len(s) - len(s.rstrip('0')) Out[3]: 5
You could simply:
I found two ways to achieve this: one has already been mentioned above, and the other is almost similar:
manipulandum.count('0') - manipulandum.rstrip('0').count('0')
But still I'm looking for a better answer.
Source: https://habr.com/ru/post/904260/More articles:Get counter using cursor.getCount () or do rawQuery using COUNT in SQL statement? - performancePHP Convert Windows-1256 encoded text to UTF-8 - phpConvert project from windows-1256 to utf-8 charset, what are the right steps? - phpHow to find and delete date based files in linux shell script without searching? - linuxDetermining if a dll is CLR.valid DLL by directly reading PE (64-bit issue) - c #How to compile objc code in Linux? - linuxWhat does indentation in a type name in the Visual Studio debugger mean? - genericsView horizontal scroll inside ViewPager - androidDigest :: CRC32 with Zlib - ruby | fooobar.comAptana 3 - Unable to update due to pydev dependency - aptana3All Articles