isalpha():, isdigit():, etc.

This is the place for queries that don't fit in any of the other categories.

isalpha():, isdigit():, etc.

Postby ooleyguy » Thu Apr 18, 2013 2:42 am

I'm to the point in my self-study that I am working with strings and string methods. I used python help on str and string and see testing methods that determine if the string contains all sorts of different characters except punctuation.

Is there a string testing method for punctuation that I missed somewhere?
ooleyguy
 
Posts: 6
Joined: Thu Apr 04, 2013 7:47 pm

Re: isalpha():, isdigit():, etc.

Postby Mekire » Thu Apr 18, 2013 3:43 am

Why would you need it? If a character isn't alpha-numeric, then it has other stuff in it. Strings are rarely made up of only punctuation so this function would almost always be False.

Just use str.isalnum(). If it returns False then there is something in your string that is neither a letter nor a number.

-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: isalpha():, isdigit():, etc.

Postby setrofim » Thu Apr 18, 2013 5:31 am

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!')
False
>>> ispunct('$$<<>...,,')
True

(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.
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm

Re: isalpha():, isdigit():, etc.

Postby Mekire » Thu Apr 18, 2013 7:11 am

Code: Select all
def ispunct(some_string):
    return not any(char.isalnum() for char in some_string)

print(ispunct("snarf123"))
print(ispunct("snarf123!"))
print(ispunct("<>#$%^@*!"))

Code: Select all
>>>
False
False
True

:D
-Mek

Edit: Bleh... I guess mine is actually more of a contains_no_alnum function as it passes things like empty strings and tabs and newlines. I just hate to use the string module.
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: isalpha():, isdigit():, etc.

Postby setrofim » Thu Apr 18, 2013 7:44 am

Code: Select all
In [1]: import string

In [2]: def ispunct1(s):
   ...:     return not any(c.isalnum() for c in s)
   ...:

In [3]: def ispunct2(s):
   ...:     return all(c in string.punctuation for c in s)
   ...:

In [4]: %timeit ispunct1(string.punctuation)
100000 loops, best of 3: 4.59 us per loop

In [5]: %timeit ispunct2(string.punctuation)
100000 loops, best of 3: 5.26 us per loop

In [6]: %timeit ispunct1(string.ascii_letters)
1000000 loops, best of 3: 909 ns per loop

In [7]: %timeit ispunct2(string.ascii_letters)
1000000 loops, best of 3: 953 ns per loop

Yup, yours is faster. Nice one, Mekire. :D

Edit:
Mekire wrote:Edit: Bleh... I guess mine is actually more of a contains_no_alnum function as it passes things like empty strings and tabs and newlines. I just hate to use the string module.

Good point. Depends what you're looking for, I guess.
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm

Re: isalpha():, isdigit():, etc.

Postby ooleyguy » Sat Apr 20, 2013 9:11 am

I can't think of a reason I'd use it. I just wondered if, since there was a way to test for all the other character types if there was a way to test for punctuation as well.

Thank you for your excellent answers.
ooleyguy
 
Posts: 6
Joined: Thu Apr 04, 2013 7:47 pm


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 8 guests