Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Python by (16.4k 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 (26.4k 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
5 answers
0 votes
2 answers
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...