Arshad Mehmood

Search the blog

    Today I Learned

    Strip non-alphabetical or non-alphanumeric characters in a string

    python

    Python characters have a function isalpha() and isalnum() which can be used to strip non-alphabetical or non-alphanumeric characters in a string.

    s = ''.join(x for x in s if x.isalnum())
    s = ''.join(x for x in s if x.isalpha())