在python中使用小数字串

时间:2011-12-22 15:44:30

标签: python string decimal

我正在尝试使用小数字的字符串,但我得到一个不需要的“0”。例如:

age = .01
print 'test%s'%(age)
print 'test' + str(age)

这两个都返回'test0.01',但我想要'test.01'。我知道有一个简单的解决方案。有什么想法吗?

3 个答案:

答案 0 :(得分:6)

age = .01
print 'test' + str(age).lstrip('0')

也适用于age > 1.0

答案 1 :(得分:2)

age = .01
print 'test%s' % str(age)[1:] if 0<age<1 else str(age)

答案 2 :(得分:0)

age = .01
print 'test' + (str(age)[1:] if 0 < age < 1 else str(age))