Re: Dynamic Zero Padding.
On Wed, Jun 8, 2011 at 4:56 AM, Terry Reedy tjre...@udel.edu wrote: On 6/7/2011 7:05 PM, John Posner wrote: You might want to try new style string formatting [1], which I think is better than the old style in this particular case: Testing {0:0{1}d}.format(42, 4) 'Testing 0042' Testing {0:0{1}d}.format(42, 9) 'Testing 00042' One cannot use a nested field in the 'name' part of the field (before the ':' (I tried), but are pretty free to nest in the actual specification part after the ':'. '{0:{1}}'.format(7,'b') '111' '{0:{1}}'.format(7,'d') '7' Thanks all for the many good suggestions - I am looking over them and reading the referenced docs. Cheers, Fred. -- http://mail.python.org/mailman/listinfo/python-list
Dynamic Zero Padding.
Hello All, I want to print some integers in a zero padded fashion, eg. : print(Testing %04i % 1) Testing 0001 but the padding needs to be dynamic eg. sometimes %05i, %02i or some other padding amount. But I can't insert a variable into the format specification to achieve the desirable padding. I would be much obliged if someone can give me some tips on how to achieve a variably pad a number. Cheers, Fred. Thanks, Fred. -- http://mail.python.org/mailman/listinfo/python-list
Re: Dynamic Zero Padding.
On Tue, Jun 7, 2011 at 2:36 PM, Friedrich Clausen f...@derf.nl wrote: Hello All, I want to print some integers in a zero padded fashion, eg. : print(Testing %04i % 1) Testing 0001 but the padding needs to be dynamic eg. sometimes %05i, %02i or some other padding amount. But I can't insert a variable into the format specification to achieve the desirable padding. I would be much obliged if someone can give me some tips on how to achieve a variably pad a number. http://docs.python.org/library/stdtypes.html#str.zfill Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: Dynamic Zero Padding.
Friedrich Clausen wrote: I want to print some integers in a zero padded fashion, eg. : print(Testing %04i % 1) Testing 0001 but the padding needs to be dynamic eg. sometimes %05i, %02i or some other padding amount. But I can't insert a variable into the format specification to achieve the desirable padding. I would be much obliged if someone can give me some tips on how to achieve a variably pad a number. :) ('%%0%dd' % (pads,)) % (n,) Probably be good to wrap it in a function. It looks kind of obscure as it is. Mel. -- http://mail.python.org/mailman/listinfo/python-list
Re: Dynamic Zero Padding.
On 6/7/2011 2:36 PM Friedrich Clausen said... Hello All, I want to print some integers in a zero padded fashion, eg. : print(Testing %04i % 1) Testing 0001 but the padding needs to be dynamic eg. sometimes %05i, %02i or some other padding amount. But I can't insert a variable into the format specification to achieve the desirable padding. I would be much obliged if someone can give me some tips on how to achieve a variably pad a number. Something like this works: for ii in ((3,12),(4,140),(5,123)): ... print (Testing %%%02ii % ii[0]) % ii[1] ... Testing 012 Testing 0140 Testing 00123 Emile -- http://mail.python.org/mailman/listinfo/python-list
Re: Dynamic Zero Padding.
Friedrich Clausen wrote: Hello All, I want to print some integers in a zero padded fashion, eg. : print(Testing %04i % 1) Testing 0001 but the padding needs to be dynamic eg. sometimes %05i, %02i or some other padding amount. But I can't insert a variable into the format specification to achieve the desirable padding. I would be much obliged if someone can give me some tips on how to achieve a variably pad a number. -- width = 3 -- print(Testing %0*i % (width, 1)) Testing 001 -- width = 7 -- print(Testing %0*i % (width, 1)) Testing 001 The '*' acts as a place holder for the width argument. ~Ethan~ -- http://mail.python.org/mailman/listinfo/python-list
RE: Dynamic Zero Padding.
Python 2.6.x 'test {0:.2f}'.format(1) 'test 1.00' 'test {0:{1}f}'.format(1,2) 'test 1.00' 'test {0:{1}f}'.format(1,.2) 'test 1.00' 'test {0:.{1}f}'.format(1,2) 'test 1.00' Ramit Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology 712 Main Street | Houston, TX 77002 work phone: 713 - 216 - 5423 -Original Message- From: python-list-bounces+ramit.prasad=jpmchase@python.org [mailto:python-list-bounces+ramit.prasad=jpmchase@python.org] On Behalf Of Friedrich Clausen Sent: Tuesday, June 07, 2011 4:37 PM To: python-list@python.org Subject: Dynamic Zero Padding. Hello All, I want to print some integers in a zero padded fashion, eg. : print(Testing %04i % 1) Testing 0001 but the padding needs to be dynamic eg. sometimes %05i, %02i or some other padding amount. But I can't insert a variable into the format specification to achieve the desirable padding. I would be much obliged if someone can give me some tips on how to achieve a variably pad a number. Cheers, Fred. Thanks, Fred. -- http://mail.python.org/mailman/listinfo/python-list This communication is for informational purposes only. It is not intended as an offer or solicitation for the purchase or sale of any financial instrument or as an official confirmation of any transaction. All market prices, data and other information are not warranted as to completeness or accuracy and are subject to change without notice. Any comments or statements made herein do not necessarily reflect those of JPMorgan Chase Co., its subsidiaries and affiliates. This transmission may contain information that is privileged, confidential, legally privileged, and/or exempt from disclosure under applicable law. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution, or use of the information contained herein (including any reliance thereon) is STRICTLY PROHIBITED. Although this transmission and any attachments are believed to be free of any virus or other defect that might affect any computer system into which it is received and opened, it is the responsibility of the recipient to ensure that it is virus free and no responsibility is accepted by JPMorgan Chase Co., its subsidiaries and affiliates, as applicable, for any loss or damage arising in any way from its use. If you received this transmission in error, please immediately contact the sender and destroy the material in its entirety, whether in electronic or hard copy format. Thank you. Please refer to http://www.jpmorgan.com/pages/disclosures for disclosures relating to European legal entities. -- http://mail.python.org/mailman/listinfo/python-list
Re: Dynamic Zero Padding.
Friedrich Clausen wrote: I would be much obliged if someone can give me some tips on how to achieve a variably pad a number. b='04' a=testing %+b+i print(a % 1) testing 0001 kind regards, m harris -- http://mail.python.org/mailman/listinfo/python-list
Re: Dynamic Zero Padding.
On 07/06/2011 22:36, Friedrich Clausen wrote: Hello All, I want to print some integers in a zero padded fashion, eg. : print(Testing %04i % 1) Testing 0001 but the padding needs to be dynamic eg. sometimes %05i, %02i or some other padding amount. But I can't insert a variable into the format specification to achieve the desirable padding. I would be much obliged if someone can give me some tips on how to achieve a variably pad a number. Testing %0*i % (4, 1) 'Testing 0001' Testing %0*i % (5, 1) 'Testing 1' -- http://mail.python.org/mailman/listinfo/python-list
Re: Dynamic Zero Padding.
Ethan Furman wrote: -- print(Testing %0*i % (width, 1)) The '*' acts as a place holder for the width argument. very nice... -- http://mail.python.org/mailman/listinfo/python-list
Re: Dynamic Zero Padding.
On Wed, Jun 8, 2011 at 7:43 AM, Mel mwil...@the-wire.com wrote: :) ('%%0%dd' % (pads,)) % (n,) Probably be good to wrap it in a function. It looks kind of obscure as it is. Would get rather pretty (read: ugly and impossible to read) if you wanted to put a literal percent sign in front of the number. :) Chris Angelico -- http://mail.python.org/mailman/listinfo/python-list
Re: Dynamic Zero Padding.
Friedrich: snip I would be much obliged if someone can give me some tips on how to achieve a variably pad a number. :) ('%%0%dd' % (pads,)) % (n,) Probably be good to wrap it in a function. It looks kind of obscure as it is. You might want to try new style string formatting [1], which I think is better than the old style in this particular case: Testing {0:0{1}d}.format(42, 4) 'Testing 0042' Testing {0:0{1}d}.format(42, 9) 'Testing 00042' HTH, John [1] http://docs.python.org/library/string.html, Section 7.1.2 String Formatting -- http://mail.python.org/mailman/listinfo/python-list
Re: Dynamic Zero Padding.
On 06/07/2011 03:01 PM, harrismh777 wrote: Ethan Furman wrote: -- print(Testing %0*i % (width, 1)) The '*' acts as a place holder for the width argument. very nice... It works for precision as well as width. wid = 10 prec = 3 num = 123.456789 print %0*.*f % (wid, prec, num) gives you - 000123.457 (It's the same as the printf() function in C.) -=- Larry -=- -- http://mail.python.org/mailman/listinfo/python-list
Re: Dynamic Zero Padding.
On 6/7/2011 7:05 PM, John Posner wrote: You might want to try new style string formatting [1], which I think is better than the old style in this particular case: Testing {0:0{1}d}.format(42, 4) 'Testing 0042' Testing {0:0{1}d}.format(42, 9) 'Testing 00042' One cannot use a nested field in the 'name' part of the field (before the ':' (I tried), but are pretty free to nest in the actual specification part after the ':'. '{0:{1}}'.format(7,'b') '111' '{0:{1}}'.format(7,'d') '7' -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list