Python string patterns
Question: I have some input: `'123Joe's amazing login'`

What I'm looking to do with that input is remind the user that is registering
to the site that he needs to make his username url friendly

So server side I would like to have some string match or comparison to see if
it is indeed url friendly, and if it is okay we are golden, if not I would
like to urlfy it and send it back to the user saying something like: "Hey this
name: 123joes-amazing-login is better"

I'm not too sure where to start looking, any suggestions?

edit:

other io could be: "this is awesome" to "this_is_awesome" etc... just need to
make sure it's url friendly!

edit 2: I wound up using this:

    
    
    username = str(request.form['username'])
    username = urllib.quote(username, '')
    if '%' in username:
    

This covers the scope of the handling i.e. users can still underscore and
dash.

Thanks for all the help.


Answer: First `pip install python-slugify`, then

    
    
    >>> import slugify
    >>> username = "123Joe's amazing login"
    >>> slugify.slugify(username)
    '123joes-amazing-login'
    

You can check if the given username is equal to the slugified version, and
then do your warning logic if they are unequal.

Now, I want to suggest to you that this is a questionable design choice. There
is no good reason why usernames shouldn't be allowed to contain spaces and
quotes. If you need them to translate into unique urls it's usually better to
tackle that problem by generating a user ID aswell as the username. This can
be done in such a way that it looks like a slugified version of the username,
by the way (although you have to be careful about uniqueness).

