Data sorting GH Python - add leading zeroes to string

Hi All, just got stuck in my partial python. Any help will be appreciated

You could have gone a little more in depth explaining what you want to do or what doesn’t work for you!
Now it’s a guessing game, but if you want to zero-pad the number behind the underscore, you can use zfill. For instance, change .format(i-i+1, str(j-j+1).zfill(2)).

The string format style operator for padding integers is: '{:04d}' (for padding with "0"s to a length of 4 digits, for example). Here’s a nice reference to use for all Python string formatting:

https://pyformat.info/#number_padding

In your case, I think you want "P{:d}_{:02d}".format(i + 1, j + 1). (The keyword arguments i=... are not needed and make things very unclear when you are reusing the names of variables from the outer scope.)

Personally I think this would look cleaner as a double list comprehension:

box = [
    ["P{:d}_{:02d}".format(i + 1,  j + 1) for j in range(count)]
    for i, count in enumerate(x)
]
2 Likes