range with variable leading zeros

Y

yhvh

I want to generate a range with variable leading zeros


x = [0010, 0210]
padding = len(x[1])

for j in range(x[0], x[1]):
print (url).join('%0(pad)d(jj)'+'.jpg') %{'pad':padding, 'jj':j}


This is syntactically incorrect, you can't insert a variable into the
string format options. Any ideas?
 
D

Dan Bishop

I want to generate a range with variable leading zeros

x = [0010, 0210]

You do realize that this is octal, right?
padding = len(x[1])

len is undefined for integers. Perhaps you meant "len(str(x[1]))".
for j in range(x[0], x[1]):
    print (url).join('%0(pad)d(jj)'+'.jpg') %{'pad':padding, 'jj':j}

This is syntactically incorrect, you can't insert a variable into the
string format options. Any ideas?

You can, however, do this:
'00123'
 
A

alex23

I want to generate a range with variable leading zeros

x = [0010, 0210]
padding = len(x[1])

for j in range(x[0], x[1]):
print (url).join('%0(pad)d(jj)'+'.jpg') %{'pad':padding, 'jj':j}

This is syntactically incorrect, you can't insert a variable into the
string format options. Any ideas?

Concatenate the parts of the pattern string first:

pat = '%0' + str(padding) + 'd'
 
Y

yhvh

x = [0010, 0210]
You do realize that this is octal, right?
It's unfortunate I choose that, the numbers go beyond octal
len is undefined for integers.  Perhaps you meant "len(str(x[1]))".
Yes sorry it was late at night :p

You can, however, do this:
'00123'

This is exactly what I needed Thank-you kindly
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top