mike
mike

Reputation: 23811

Using strings of decimals in python

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?

Upvotes: 1

Views: 126

Answers (3)

Xavier Combelle
Xavier Combelle

Reputation: 11205

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

Upvotes: 2

eumiro
eumiro

Reputation: 212885

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

Works for age > 1.0 as well.

Upvotes: 6

BenH
BenH

Reputation: 2120

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

Upvotes: 0

Related Questions