Mekire wrote:Just use str.isalnum(). If it returns False then there is something in your string that is neither a letter nor a number.
This would return False if there is at least one
punctuation character. If you wanted to test if all
characters were punctuation, i.e. an equivalent of isnum or isalpha, you can do it like this:
- Code: Select all
>>> import string
>>> def ispunct(s):
... return all(c in string.punctuation for c in s)
>>> ispunct('Hello, World!')
(a more efficient but less readable method would involve checking if all the ordinals are in the right ranges). How often you would come across a stuation when you'd want to do this, is another question.