shorten this: if char in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz":
MRAB
google at mrabarnett.plus.com
Tue Jun 24 18:34:46 EDT 2008
On Jun 24, 7:59 pm, "Guilherme Polo" <ggp... at gmail.com> wrote:
> On Tue, Jun 24, 2008 at 3:47 PM, bruno.desthuilli... at gmail.com
>
>
>
> <bruno.desthuilli... at gmail.com> wrote:
> > On 24 juin, 20:32, cirfu <circularf... at yahoo.se> wrote:
> >> if char in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz":
>
> >> cant i write something like:
> >> if char in "[A-Za-z]":
>
> > Nope. But there are other solutions. Here are two:
>
> > # 1
> > import string
>
> > if char in string.letters:
> > print "yay"
>
> > # 2
> > import re
> > exp = re.compile(r'[A-Za-z]')
>
> > if exp.match(char):
> > print "yay"
>
> Let me post another one, and longer:
>
> if ord(somechar) in range(ord('A'), ord('Z') + 1) + range(ord('a'),
> ord('z') + 1):
> ...
>
And another:
if 'A' <= somechar <= 'Z' or 'a' <= somechar <= 'z':
...
More information about the Python-list
mailing list