Python Forum
Why isalnum() didn't output False when I use punctuations, spaces, etc in input?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why isalnum() didn't output False when I use punctuations, spaces, etc in input?
#3
(Jun-14-2024, 09:28 PM)deanhystad Wrote: isalnum() does return False for spaces.
for letter in "aA1 :/":
    print(letter, letter.isalnum())
Output:
a True A True 1 True False : False / False
Your problem must be something else.

You use slices, but you don't use them effectively. For example, this:
for i in s[0:2]:
    if i.isnumeric():
        return False
Could be written like this:
if any(c.isnumeric() for c in s[:2]
    return False
Or better yet
if not s[:2].isalpha():
    return False
Similarly, this loop should be removed:
for k in s:
    if k.isalphanum():
        ...
    else:
        return False
And replaced withL
if not s.isalnum():
    return False
Your program is also difficult to read because there is so much distance between a test and the function returning a False result because of the test, It would be easier to read if written like this:

pseudo code
def valid_plate(plate):
    if length of plate not valid:
        return False
    if all characters are not alphanumeric:
        return False
    if the first two characters are not alphabetic:
        return False
    if the first number is "0":
        return False
    if any charcter following the first number is not a number
        return False
    return True  # plate passed all the tests.


I appreciate you sharing this information. I find it handy.
boxing random
Reply


Messages In This Thread
RE: Why isalnum() didn't output False when I use punctuations, spaces, etc in input? - by murmurgeneral - Jun-26-2024, 08:02 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Code to use output from arduino as input (help) Updownrightleft 0 2,313 Nov-03-2018, 11:04 PM
Last Post: Updownrightleft
  Unable to output the indices from an input file rainbow2312 1 3,067 Nov-18-2017, 05:53 PM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020