Which method to check if string index is queal to character.

Chris Angelico rosuav at gmail.com
Mon Dec 28 17:05:43 EST 2020


On Tue, Dec 29, 2020 at 8:57 AM dn via Python-list
<python-list at python.org> wrote:
> After such disparagement it is worth remembering that there are checks
> and there are checks! It depends upon the purpose of the check, or the
> level-of-detail/accuracy desired!
>
> When accepting user-data it *is* worth (even "necessary") performing a
> 'sanity-check'. Per @Chris, if the user doesn't enter two 'words'
> separated by an @-sign, and the second 'word' doesn't include at least
> one dot/period/stop, then it seems quite probable that our user has made
> a typo! This is why some sites require an email address to be entered
> twice. (but copy-paste anyone?)

I wouldn't even ask for a dot in the second half, actually. Yes, it's
uncommon to have no dot, but it's fully legal, and not worth wrestling
with. Considering the vast number of typos that *wouldn't* trip a
filter, and the relatively small number that would, it's generally not
worth putting too much validation in.

Checking for the presence of "@" is a good way to check that it's an
email address and not, say, the URL of someone's webmail service, but
other than that, there's really not a lot that's worth checking.

OTOH, if you're trying to recognize email addresses in plain text
(say, in chat messages) so that you can autolink them, that's
completely different. Same with URLs (eg https://example.com/foo) -
you, as a human, can see that the URL should have ended at the "/foo",
but technically "/foo)" is perfectly legal. So if you're validating,
"/foo)" should be permitted, but if you're detecting, it should stop
at "/foo".

ChrisA


More information about the Python-list mailing list