Put a capture group around the part that you want to preserve, and then include a reference to that capture group within your replacement text.
re.sub(r'(\_a)?\.([^\.]*)$' , r'_suff.\2',"long.file.name.jpg")
Put a capture group around the part that you want to preserve, and then include a reference to that capture group within your replacement text.
re.sub(r'(\_a)?\.([^\.]*)$' , r'_suff.\2',"long.file.name.jpg")