applying a filter on a string in python -
i have user typing in username , want valid strings pass through, meaning characters in [a-za-z0-9]. pretty new python , unsure of syntax.
here's example of want in code, check through username , return false upon illegal character.:
def _checkinput(input): char in input: if !(char in [a-za-z0-9]): return false return true
thanks!
you need isalnum:
>>> name = raw_input('enter name: ') enter name: foo_bar >>> name.isalnum() false >>> name = raw_input('enter name: ') enter name: foobar >>> name.isalnum() true
Comments
Post a Comment