473,320 Members | 1,902 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

shorten this: if char in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvw xyz":

if char in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvw xyz":

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

?
Jun 27 '08 #1
9 1837
cirfu wrote:
if char in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvw xyz":

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():

--
Brian

Jun 27 '08 #2
On 24 juin, 20:32, cirfu <circularf...@yahoo.sewrote:
if char in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvw xyz":

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"

Jun 27 '08 #3
On Tue, Jun 24, 2008 at 3:47 PM, br*****************@gmail.com
<br*****************@gmail.comwrote:
On 24 juin, 20:32, cirfu <circularf...@yahoo.sewrote:
>if char in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvw xyz":

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):
...
--
-- Guilherme H. Polo Goncalves
Jun 27 '08 #4
another way:

import string

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

[]'s
- Walter
Jun 27 '08 #5
On Jun 24, 7:59*pm, "Guilherme Polo" <ggp...@gmail.comwrote:
On Tue, Jun 24, 2008 at 3:47 PM, bruno.desthuilli...@gmail.com

<bruno.desthuilli...@gmail.comwrote:
On 24 juin, 20:32, cirfu <circularf...@yahoo.sewrote:
if char in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvw xyz":
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':
...
Jun 27 '08 #6
On Jun 25, 4:32 am, cirfu <circularf...@yahoo.sewrote:
if char in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvw xyz":

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":
Jun 27 '08 #7
On Jun 24, 5:36 pm, John Machin <sjmac...@lexicon.netwrote:
On Jun 25, 4:32 am, cirfu <circularf...@yahoo.sewrote:
if char in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvw xyz":
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.

Jun 27 '08 #8
On Jun 25, 9:06 am, s0s...@gmail.com wrote:
On Jun 24, 5:36 pm, John Machin <sjmac...@lexicon.netwrote:
On Jun 25, 4:32 am, cirfu <circularf...@yahoo.sewrote:
if char in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvw xyz":
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?
Jun 27 '08 #9
Guilherme Polo <gg****@gmail.comwrote:
Let me post another one, and longer:

if ord(somechar) in range(ord('A'), ord('Z') + 1) + range(ord('a'),
ord('z') + 1):
...
That is very inefficient! Every time it is run it creates two lists
then joins them then throws the whole lot away!

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: godfather2 | last post by:
this should be an easy one ... what is the meaning of "1u", for instance: #define DUMMY (1U<<31) as opposed to just: #define DUMMY (1<<31)
388
by: maniac | last post by:
Hey guys, I'm new here, just a simple question. I'm learning to Program in C, and I was recommended a book called, "Mastering C Pointers", just asking if any of you have read it, and if it's...
145
by: Sidney Cadot | last post by:
Hi all, In a discussion with Tak-Shing Chan the question came up whether the as-if rule can cover I/O functions. Basically, he maintains it can, and I think it doesn't. Consider two...
42
by: Prashanth Badabagni | last post by:
Hi, Can any body tell me how to print "hello,world" with out using semicolon Thanks in advance .. Bye Prashanth Badabagni
134
by: jacob navia | last post by:
Hi Suppose you have somewhere #define BOOL int and somewhere else typedef BOOL int;
5
by: Aleramo | last post by:
I hope someone could say me where can i find the meaning of: gsl: sinint.c:359: ERROR: domain error Default GSL error handler invoked. Aborted or where i can find it. I don't understand the...
23
by: Abhi | last post by:
Hi.. I wanted the C source code in machine readable format for the book "Numerical Recipes in C". I got hold of the pdf version of the book somehow. Does anyone have the complete C code of the...
34
by: Frederick Gotham | last post by:
Is the domestic usage of the C "for" loop inefficient when it comes to simple incrementation? Here's a very simple program that prints out the bit-numbers in a byte. #include <stdio.h> #include...
6
by: stephen | last post by:
I found myself writing: for f in : print 'Processing datafile %s' % f but I was wishing that I could have instead written: for f in in datafiles if '.txt' in f: print 'Processing datafile...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.