0

This code ...

print( "PO #{}".format('PUR-ORD-2021-00001'.lstrip('PUR-ORD-2021-')))
print( "PO #{}".format('PUR-ORD-2021-00002'.lstrip('PUR-ORD-2021-')))
print( "PO #{}".format('PUR-ORD-2021-00003'.lstrip('PUR-ORD-2021-')))
print( "PO #{}".format('PUR-ORD-2021-00008'.lstrip('PUR-ORD-2021-')))
print( "PO #{}".format('PUR-ORD-2021-00018'.lstrip('PUR-ORD-2021-')))

... returns:

PO #
PO #
PO #3
PO #8
PO #8

I'm no Python expert, but in my experience so far Python's results are always eminently sensible. But this? I can't make sense of it at all.

What's going on here?


Version:

me@mine:~$ python
Python 3.8.10 (default, Jun  2 2021, 10:49:15) 
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 
Martin Bramwell
  • 2,003
  • 2
  • 19
  • 35
  • 1
    `lstrip` takes an iterable and strips each character of the iterable from the left. Since `'PUR-ORD-2021-'` is an iterable it strips every occurance of each character. (`'P'`, `'U'`, etc..) https://docs.python.org/3/library/stdtypes.html#str.lstrip – Axe319 Jul 08 '21 at 17:05
  • 1
    OOOH! So it is stripping all 0s, 1s and 2s because of the 2021! Got it! Thanks so much. – Martin Bramwell Jul 08 '21 at 17:08
  • 1
    No problem. It's a common mistake that leads to some nasty bugs. – Axe319 Jul 08 '21 at 17:09
  • I'd add it as an answer but it'll probably get marked as a dupe. https://stackoverflow.com/questions/34544247/understanding-pythons-lstrip-method-on-strings – Axe319 Jul 08 '21 at 17:13

1 Answers1

1

You can just replace part of the string to get the order number:

original_string = 'PUR-ORD-2021-00001'
order_number = original_string.replace('PUR-ORD-2021-', '')
print('PO #', order_number)
# PO # 00001

Hammurabi
  • 1,141
  • 1
  • 4
  • 7