1

I want to convert a number in python into a 3 character string, with prefix 0s. For example:

8 = "008" 12 = "012" 6 = "006" 

Thanks

3 Answers 3

7

Just use:

format(number, '03') 
Sign up to request clarification or add additional context in comments.

Comments

5

My name is format. String format.

>>> '{:03d}'.format(7) '007' 

1 Comment

Relaying a +1 from The British Superspy himself
1

There is a build-in string method str.zfill(), examples:

>>> '12'.zfill(5) '00012' >>> '-3.14'.zfill(7) '-003.14' >>> '3.14159265359'.zfill(5) '3.14159265359' 

see: http://docs.python.org/3.0/library/stdtypes.html

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.