-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)). 
6
  • Please use the new string formatting and you won't have a lot of these problems. Commented Nov 27, 2017 at 2:38
  • 1
    You can use '1'.zfill(2) Commented Nov 27, 2017 at 2:38
  • @user1767754: Could you please give me a link Commented Nov 27, 2017 at 2:38
  • 2
    '{value:02d}'.format(value=1) Commented Nov 27, 2017 at 2:42
  • 2
    Or you could upgrade to Python 3.6 and use f-strings, Eg f'{value1:02}' Commented Nov 27, 2017 at 2:45

3 Answers 3

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).

Sign up to request clarification or add additional context in comments.

8 Comments

This is surely a more pythonic way than the other one.
How to go about in Python 2.7 ?
The new string format codes also work in Python 2.7.
@DavidZ: It doesn't! It still does not recognise the 0 in 01
@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
|
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

13 Comments

I love grumpy downvoters
I was about to iteratively make my answer better
There's no need for a separate .zfill call: .format can do that operation. Eg, "{0:02}".format(1)
Um, your answer is now a carbon copy of DavidZ's...
Still iterating, I'm not the downvoter.... but keep in mind that taking a solution someone else suggested first as yours is a little unfair to that other person.
|
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.

5 Comments

@cᴏʟᴅsᴘᴇᴇᴅ, my english isn't really good and the translator think you are going to kill someone with "dupe target"... but, you're welcome, I guess.
I want to use it for more than one value
@ThePredator, Could you explain better? I don't understand what are you meaning.
I have edited my question, please look
@ThePredator, better?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.