C
cirfu
if char in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz":
cant i write something like:
if char in "[A-Za-z]":
?
cant i write something like:
if char in "[A-Za-z]":
?
cirfu said:if char in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz":
cant i write something like:
if char in "[A-Za-z]":
if char in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz":
cant i write something like:
if char in "[A-Za-z]":
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"
And another: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 stringif 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):
...
if char in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz":
cant i write something like:
if char in "[A-Za-z]":
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":
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.
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.