-1

Ok so basically this is what I know, and it does work, using Python3:

color="Red1 and Blue2!"
color[2]=="d"
True

What I need is that when I call any position, (which inputs any single character Lower or Upper case in the comparison), into the brackets "color[ ]" and compare it to match only with "Lower or Upper case letters" excluding all numbers and characters (.*&^%$#@!). in order words something to the effects below:

color="Red1 and Blue2!"
if color[5]==[a-zA-z]:
     doSomething
else:
     doSomethingElse

Of course what I just listed above does not work. Perhaps my syntax is wrong, perhaps it just cant be done. If I only use a single letter on the "right" side of the equals, then all is well, But like I said I need whatever single letter is pulled into the left side, to match something on the right.

First off I wan't to make sure that its possible to do, what I'm trying to accomplish? 2nd, if it is indeed possible to do then have this accomplished "Without" importing anything other then "sys".

If the only way to accomplish this is by importing something else, then I will take a look at that suggestion, however I prefer not to import anything if at all possible.

I'v searched my books, and a whole other questions on this site and I can't seem to find anything that matches, thanks.

3 Answers3

3

For the case of looking for letters, a simple .isalpha() check:

if color[5].isalpha():

will work.

For the general case where a specific check function doesn't exist, you can use in checks:

if color[5] in '13579':  # Checks for existence in some random letter set

If the "random letter set" is large enough, you may want to preconvert to a frozenset for checking (frozenset membership tests are roughly O(1), vs. O(n) for str, but str tests are optimized enough that you'd need quite a long str before the frozenset makes sense; possibly larger than the one in the example):

CHARSET = frozenset('13579adgjlqetuozcbm')

if color[5] in CHARSET:

Alternatively, you can use regular expressions to get the character classes you were trying to get:

import re

# Do this once up front to avoid recompiling, then use repeatedly
islet = re.compile('^[a-zA-Z]$').match  
...
if islet(color[5]):
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
2

This is where isalpha() is helpful.

color="Red1 and Blue2!"
if color[5].isalpha():
     doSomething
else:
     doSomethingElse

There's also isnumeric(), if you need numbers.

Morgan Thrapp
  • 9,748
  • 3
  • 46
  • 67
0

Not really sure why you'd require not importing anything from the standard libraries though.

import string

color="Red1 and Blue2!"
if color[5] in string.ascii_letters:
   print("do something")
else:
   print("do something else")
Takis
  • 726
  • 5
  • 11
  • While that's a good pattern for general purpose testing if a letter is part of an arbitrary set of characters (and `string.ascii_letters` is useful for other purposes, like iterating specific letters or the like), and necessary if you want only ASCII letters, `.isalpha()` is more predictable (same runtime for all hits and misses), and will also include non-ASCII letters as "alphabetic" characters, which you usually want. – ShadowRanger Dec 10 '15 at 00:57
  • @ShadowRanger You're right, but I wanted to give a simpler answer then when using the regex package or listing all characters explicitly. I'd have prefered to add the `string.ascii_letters` to your answer as to make it more complete. – Takis Dec 10 '15 at 01:01
  • Yes it wasnt my choice, it was the requirement I was given, involves not importing anything, these reponses have helped. I still have more code to write. – Jaymes Deen Dec 10 '15 at 01:16