For example i’m printing something like:
print ('Number of Elephants less than %s: %s' % (str(floatB),str(len(myList))))
That ends with 4 brackets and doesn’t feel very pythonic! Is there a better way? It’s not really a performance thing, but I’m just going through the code and want to make it a little cleaner than that.
You can lose the
str()calls:This has exactly the same effect as your original code, since
%sresults in an automatic call tostr()for the argument in question.Note that instead of
%syou could use the more specific%d(for the integer) and%e,%E,%f,%F,%gor%Gfor the float. These format specifiers are more flexible in that they allow you to specify the number of digits to print etc.