0

Question Is there way add numbers 0-9 and 10-99 to the end of a number like e000 that works for both single e0001 and double digits like e0010 instead of the expected result of e00010?

Goal I am looking to add a value to the end of opponent_movefour-e000 before the extension .png that can handle adding 00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12, 13, 14, 15.

What I tried If I add a %d on to the end of e000%d I can get the values e0000, e0001, e0002, e0003, e0004, e0005, e0006, e0007, e0008, e0009 which is expected.

    self.opponent_movetwoImages = []
    for i in range(10):
        imgName = "opponentImages/opponent_movefour-e000%d.png" % i
        tmpImage = pygame.image.load(imgName)
        tmpImage = tmpImage.convert()
        transColor = tmpImage.get_at((1, 1))
        tmpImage.set_colorkey(transColor)
        self.opponent_movefourImages.append(tmpImage)

%d works for numbers 10 or under as expected but what if I need a number 11-20

And I understand that having it as 15 it creates e00010, e00011, e00012, e00013, e00014, e00015

Is there a way to get e0010, e0011, e0012, e0013, e0014, e0015

    self.opponent_movefourImages = []
    for i in range(15):   #needs to be 15 
        imgName = "opponentImages/opponent_movefour-e000%d.png" % i
        tmpImage = pygame.image.load(imgName)
        tmpImage = tmpImage.convert()
        transColor = tmpImage.get_at((1, 1))
        tmpImage.set_colorkey(transColor)
        self.opponent_movefourImages.append(tmpImage)

Couldn't open opponentImages/opponent_movefour-e00010.png

awesoon
  • 32,469
  • 11
  • 74
  • 99
john taylor
  • 1,080
  • 15
  • 31
  • possible duplicate of [How to format print output into fixed width?](http://stackoverflow.com/questions/8450472/how-to-format-print-output-into-fixed-width) – awesoon Aug 31 '15 at 14:39
  • 1
    This, actually, more relevant: [Fixed width number formatting python 3](http://stackoverflow.com/questions/6869999/fixed-width-number-formatting-python-3) – awesoon Aug 31 '15 at 14:40
  • soon, the first one I don't understand and the second one would not work in my case as I already added the 0 – john taylor Aug 31 '15 at 15:01
  • @soon, the Fixed width number formatting python 3 say to [Prefix the width with a 0](http://stackoverflow.com/questions/6869999/fixed-width-number-formatting-python-3) but was already prefixed with 3 zeros, 000 so it will not work. – john taylor Aug 31 '15 at 15:05
  • So, couldn't you move zeroes to the *format* part of the string? – awesoon Aug 31 '15 at 15:21

1 Answers1

1

Use -e%04d instead of -e%d. The 0 means 0 padded, and the 4 means the minimal width is 4 digits.

For instance:

>>> 'e%04d' % 5
0005
>>> 'e%04d' % 55
0055

More info on string formating: https://docs.python.org/3.1/library/string.html#format-specification-mini-language

serge-sans-paille
  • 2,109
  • 11
  • 9
  • Thanks, I understand this perfectly, I read those strings documents earlier but never made the connection of single digits and double digits numbers being referred to as width. Even after read those other possible duplication question I would not of figured it out. – john taylor Aug 31 '15 at 14:57