I wrote a simple class that takes an input zip or postal code and either returns that value or zero-pads it out to five digits if it happens to be all-numeric and less than 5 digits long.
Why doesn't my code work?
import re
class ZipOrPostalCode:
def __init__(self, data):
self.rawData = data
def __repr__(self):
if re.match(r"^\d{1,4}$", self.rawData):
return self.rawData.format("%05d")
else:
return self.rawData
if __name__ == "__main__":
z=ZipOrPostalCode("2345")
print(z)
The output I expect is 02345. It outputs 2345.
Running it in the debugger, it is clear that the regular expression didn't match.