0

I have a string like 'H897b Site', and I want to test that string to make sure it only contains letters and numbers and no special characters.

So in cases like 'H897b Sit$e' and 'H8(&b Site' I want to be able to flag it.

What I am trying to accomplish is to loop through a column in a table and check for typing errors from the data entry person.

jscs
  • 63,694
  • 13
  • 151
  • 195
Mike
  • 4,099
  • 17
  • 61
  • 83
  • 1
    http://docs.python.org/2/howto/regex.html – Jacinda Jun 05 '13 at 17:40
  • 1
    do spaces count as letter and numbers? – Joran Beasley Jun 05 '13 at 17:51
  • @JoranBeasley Yes, spaces are fine. In my example above, the space is acceptable. The first part of the string before the space is the is the actual site name. The ' Site' will always come after the actual site name. Does that clear up your question? – Mike Jun 05 '13 at 18:41
  • Mike: @Marc is right and _your_ question is what is not constructive. See the Stack Exchange [Community FAQ](http://meta.stackexchange.com/questions/7931/faq-for-stack-exchange-sites) about asking questions. All see [Why are some questions closed?](http://stackoverflow.com/helpcenter/closed-questions). – martineau Jun 05 '13 at 18:55
  • 2
    Other duplicates: http://stackoverflow.com/questions/89909/in-python-how-to-i-verify-that-a-string-only-contains-letters-numbers-undersco http://stackoverflow.com/questions/10944438/how-do-i-check-if-a-string-only-contains-alphanumeric-characters-and-dashes http://stackoverflow.com/questions/4939669/how-do-i-write-a-regex-function-in-python-that-checks-if-a-user-has-only-letters – jscs Jun 05 '13 at 22:21

2 Answers2

1

You can loops through the string and make checks like

if 'z' >= c >= 'a'

Similarly for numerals etc.

iruvar
  • 22,736
  • 7
  • 53
  • 82
Aseem Bansal
  • 6,722
  • 13
  • 46
  • 84
1
    print all(part.isalnum() for part in my_string.split())
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179