This is still convoluted. `return value.replace(' ', '_')` is enough, there is no reason to do all those checks.