def normalise_string ( string ) : string = ( string . strip ( ) ) . lower ( ) return re . sub ( r'\W+' , '_' , string )
how to replace spaces with underscores in python
Strips trailing whitespace from string lowercases it and replaces spaces with underscores
def normalise_string(string):
    """ Strips trailing whitespace from string, lowercases it and replaces
        spaces with underscores
    """
    string = (string.strip()).lower()
    return re.sub(r'\W+', '_', string)