How do I replace whitespaces with underscore?

前端 未结 13 2165

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:22

    Python has a built in method on strings called replace which is used as so:

    string.replace(old, new)
    

    So you would use:

    string.replace(" ", "_")
    

    I had this problem a while ago and I wrote code to replace characters in a string. I have to start remembering to check the python documentation because they've got built in functions for everything.

提交回复
热议问题