shorten this: if char in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz":

C

cirfu

if char in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz":

cant i write something like:
if char in "[A-Za-z]":

?
 
B

Brian Victor

cirfu said:
if char in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz":

cant i write something like:
if char in "[A-Za-z]":

Either of the following should do what you want, without resorting to
regular expressions:

import string
if char in string.letters:

or

if char.isalpha():
 
B

bruno.desthuilliers

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"
 
G

Guilherme Polo

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):
...
 
W

Walter Cruz

another way:

import string

if char in string.ascii_letters:
print('hello buddy!')

[]'s
- Walter
 
M

MRAB

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':
...
 
J

John Machin

if char in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz":

cant i write something like:
if char in "[A-Za-z]":

You can write that if you want to, but it's equivalent to
if char in "zaZa]-[":
i.e. it doesn't do what you want.

This gives the same reuslt as your original code, unaffected by
locale:

if "A" <= char <= "Z" or "a" <= char <= "z":
 
S

s0suk3

if char in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz":
cant i write something like:
if char in "[A-Za-z]":

You can write that if you want to, but it's equivalent to
if char in "zaZa]-[":
i.e. it doesn't do what you want.

This gives the same reuslt as your original code, unaffected by
locale:

if "A" <= char <= "Z" or "a" <= char <= "z":

But doesn't that rely on the underlying character set? It's like
performing math on C char's (maybe that's what the interpreter does
internally?). If that's the case, using 'char.isalpha()' or 'char in
string.letters' or regex's would be better.
 
J

John Machin

if char in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz":
cant i write something like:
if char in "[A-Za-z]":
You can write that if you want to, but it's equivalent to
if char in "zaZa]-[":
i.e. it doesn't do what you want.
This gives the same reuslt as your original code, unaffected by
locale:
if "A" <= char <= "Z" or "a" <= char <= "z":

But doesn't that rely on the underlying character set?

Unless there is a Python implementation using EBCDIC, different
underlying character set that differs from ASCII in A..Z and a..z is
*not* a practical concern.
It's like
performing math on C char's (maybe that's what the interpreter does
internally?). If that's the case, using 'char.isalpha()' or 'char in
string.letters' or regex's would be better.

You should be asking the OP what he really wants ... precisely those
letters? alphabetic, in what locale?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,444
Messages
2,571,709
Members
48,796
Latest member
Greg L.
Top