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
Django has a 'slugify' function which does this, as well as other URL-friendly optimisations. It's hidden away in the defaultfilters module.
>>> from django.template.defaultfilters import slugify
>>> slugify("This should be connected")
this-should-be-connected
This isn't exactly the output you asked for, but IMO it's better for use in URLs.