I'm trying to use a string of a decimal, but I am getting an unwanted "0.". For example:
age = .01
print 'test%s'%(age)
print 'test' + str(age)
These both return 'test0.01', but I want 'test.01'. I know there is a simple solution. Any thoughts?
age = .01
print 'test' + str(age).lstrip('0')
Works for age > 1.0 as well.
-1 < age < 0. Then again it may not.age = .01
print 'test%s' % str(age)[1:] if 0<age<1 else str(age)
x such that -1 < x < 0.age = .01
print 'test' + (str(age)[1:] if 0 < age < 1 else str(age))
x outside of 0 < x < 1. Safe: replace "0" with "" if followed by "." and not preceded by a digit (easy with regexp: lookahead and negative lookbehind). Or just use this, for numbers between 0 and 1, and a similar action for numbers between -1 and 0.age = five either. I'm not claiming to have put forward the greatest python code ever written, just trying to give him something to work with.if yet. This fixes it for positive numbers; -1 < x < 0 still won't work (although the solution is simple and analogous, and just an additional if away).