0 votes
1 view
in Python by (12.1k points)

How would I get a whole number to fill 0's to a fixed width in python 3.2 utilizing the format attribute?

Example:

a = 1

print('{0:3}'.format(a))

I want '001',  but it  gives '1'.

In python 2.x, this can be done using

print "%03d" % number.

I also checked the documentation of python3, but I wasn't able to this. 

For documentation, click on this link 

1 Answer

0 votes
by (25.2k points)

Just prefix the width with 0:

>>> '{0:03}'.format(1)

'001'

In the recent versions of python,  you don't need the place-marker.

>>> '{:03}'.format(1)

'001'

Want to learn more concepts related to Python? Come and Join: python certification course

For more details, do check out:

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...