Lucas Lucas - 1 year ago 132
Python Question

How do I replace whitespaces with underscore and vice versa?

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_connected"

I am using Python with Django. Can this be solved using regular expressions?

Answer Source

You don't need regular expressions, Python has a string method that does what you need:

mystring.replace (" ", "_")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download