OP использует python, но в javascript (то, что нужно соблюдать осторожность, так как синтаксис похожи.
// only replaces the first instance of ' ' with '_'
"one two three".replace(' ', '_');
=> "one_two three"
// replaces all instances of ' ' with '_'
"one two three".replace(/\s/g, '_');
=> "one_two_three"