Qumeric
Qumeric

Reputation: 518

Converting numbers that start with zeros

Can anyone explain the following to me please.

i = 700001
z = str(i % 100000)

z='1', but i want to z='00001'. How i can get it?

Upvotes: 1

Views: 149

Answers (4)

Don
Don

Reputation: 17636

See Python string formatting:

z = '%05d'%(i % 100000)

Upvotes: 2

D.Shawley
D.Shawley

Reputation: 59583

Use str.rjust

>>> s = '1'
>>> s.rjust(5, '0')
'00001'

Upvotes: 2

Tim Pietzcker
Tim Pietzcker

Reputation: 336408

Another solution:

>>> z.zfill(5)
'00001'

Upvotes: 4

Marcelo Cantos
Marcelo Cantos

Reputation: 185972

Do this:

z = '{:05}'.format(i % 100000)

Upvotes: 6

Related Questions