-3

To have specific number of digits in string formatting, I am aware that it can be done in this way:

In [18]: hours = 01

In [19]: "%.2d" %(hours)
Out[19]: '01'

In [20]: "%.2f" %(hours)
Out[20]: '1.00'

But my case is a bit different. I am using specific keys to denote the values, for example:

for filename in os.listdir('/home/user/zphot_01/')

Here I want to have different values for the '01', i.e.

for filename in os.listdir('/home/user/zphot_{value1}/'.format(value1=some_number):

When I use the above method with some_number = 01, it does not take into account the 0 and so my folder is not recognised.

EDIT:

Most of the answers are for only one value, however, I want to have more than one key value, i.e.:

for filename in os.listdir('/home/user/zphot_{value1}/zphot_{value2}'.format(value1=some_number1,value2=some_number2)). 
Srivatsan
  • 9,225
  • 13
  • 58
  • 83

3 Answers3

3

The new format string syntax allows you to use format specifiers, just like the old %-based syntax. The format specifiers you can use are similar, not exactly the same in all cases (I think), but as far as I know, anything you could do with the old syntax can also be done with the new syntax.

All you have to do is put the format specifier inside the formatting expression, separated from the field name/number by a colon. In this case, you could use {value1:02d}, where 02d is the code to get a zero-filled (0) width-2 (2) representation of an integer (d).

David Z
  • 128,184
  • 27
  • 255
  • 279
  • This is surely a more pythonic way than the other one. – Sraw Nov 27 '17 at 02:43
  • How to go about in Python 2.7 ? – Srivatsan Nov 27 '17 at 02:46
  • The new string format codes also work in Python 2.7. – David Z Nov 27 '17 at 02:47
  • 1
    @DavidZ: It doesn't! It still does not recognise the `0` in `01` – Srivatsan Nov 27 '17 at 02:48
  • 2
    @ThePredator What is that `0` supposed to mean? Are you talking about when you set `hours = 01`? You should know that in code, `1` and `01` represent exactly the same value – David Z Nov 27 '17 at 02:51
  • This votes were 5-0 initially... how is it 3-2 now? – cs95 Nov 27 '17 at 02:52
  • @cᴏʟᴅsᴘᴇᴇᴅ I dunno, I also found it odd and somewhat disappointing that the score on my answer dropped from 5 to 1 (now 2) and the votes on user1767754's answer went up from 0 to 2 (now 3) after their answer was edited to use the technique I suggested... but voters are fickle. Not much to do about it, I suppose, unless someone offers a reason for the downvote (and/or retracted upvotes) on my answer that is something I can fix. – David Z Nov 27 '17 at 02:54
  • @DavidZ: I have a folder named `zphot_01`, and another inside this folder named `zphot_02` and so on.. Now I want to specify `01` to be the first value and `02` to be second value as you can see `zphot_` is constant in both the folders. This was my question – Srivatsan Nov 27 '17 at 05:11
1
print("{0:02}".format(1))
>>0001

Just learnt from other answers and commentators that we don't need zfill but can use the expression :02 to give the padding.

Expand to more positions:

print("{0:02}_{1:02}".format(1, 2))
>>0001_0002
user1767754
  • 23,311
  • 18
  • 141
  • 164
1

There are a lot of ways. Look this answer.

This is my subjetive opinion, but I have ordered them by worst to best.

>>> '1'.zfill(2)
'01'
>>> '%02d' % 1
'01'
>>> '%02s' % '1'
'01'
>>> '{0:0>2}'.format(1)
'01'
>>> '{0:02d}'.format(1)
'01'
>>> '{:02d}'.format(1)
'01'
>>> f'{1:02}'
'01'

Then, you have to combine that with your current string, nothing really complicate.

Edit:

I am not sure what the OP is asking with his edit exactly but:

for filename in os.listdir('/home/user/zphot_{value1}/zphot_{value2}'.format(value1=some_number1,value2=some_number2)).

Can be changed by a lot of ways, I'll give some examples:

>>> number_first, number_second = '1', '2'
>>> '/home/user/zphot_{value1}/zphot_{value2}'.format(value1 = number_first.zfill(2), value2 = '2'.zfill(2))
'/home/user/zphot_01/zphot_02'
>>> '/home/user/zphot_{}/zphot_{}'.format('1'.zfill(2), number_second.zfill(2))
'/home/user/zphot_01/zphot_02'
>>> f'/home/user/zphot_{{number_first:02}}/zphot_{2:02}'
'/home/user/zphot_01/zphot_02'    
>>> '/home/user/zphot_%02d/zphot_%02s' % (1, '2')
'/home/user/zphot_01/zphot_02'
>>> '/home/user/zphot_{:02d}/zphot_{:02d}'.format(1, 2)
'/home/user/zphot_01/zphot_02'

Etc.

Ender Look
  • 2,303
  • 2
  • 17
  • 41