For example:
print('items %05d'%12)
would print 00012.
Can the amount of padding be specified by a variable instead of a literal? I believe python 2.6+ has the .format function but what are the options with python 2.5?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can replace the width number with a
*and the Python%substitution will expect an integer value, and use that for the width. Since you will now have at least two values for the%operator (one width and one actual value) you will need to make a tuple to pass them together.If you leave out the
0before the*you will get padding with spaces rather than0.Documented here:
http://docs.python.org/release/2.5.2/lib/typesseq-strings.html
Section 3.6.2, rule 4.