import re s = "test001"print(re.sub(r'^(\D+)(\d+)$', lambda x: rf'{x.group(1)}-{x.group(2).zfill(5)}', s))
Standard input is empty
test-00001
The brand new service which powers Ideone!
Widget for compiling and running the source code in a web browser!