How do I replace whitespaces with underscore?

前端 未结 13 2146

I want to replace whitespace with underscore in a string to create nice URLs. So that for example:

\"This should be connected\" becomes \"This_should_be_conn         


        
13条回答
  •  自闭症患者
    2020-11-29 16:23

    You don't need regular expressions. Python has a built-in string method that does what you need:

    mystring.replace(" ", "_")
    

提交回复
热议问题