The number was actually 39 lashes, and the reasoning behind it was practical rather than merciful in any generous sense.
Roman practice held that forty lashes could easily kill a prisoner, so the punishment was capped one short of that figure to keep the condemned alive for the crucifixion that followed.
It sounds like a strange bit of bookkeeping, but that is the explanation that has come down to us.