How To Check For [a-z] In String?

H

Hank Kingwood

This is probably an easy question, but I can't find a function (maybe my
syntax is off...) to search for [a-z] in a string. If someone would
help out, I'd appreciate it!

Also, how would you recommend searching for the following in the same
string:
[a-z]
[A-Z]
[0-9]
-

My approach would be to perform four separte checks, but I'm thinking
there might be some cool approach that would use a dictionary or array.
Ideas?

Thanks,
Hank
 
J

Jay Dorsey

Hank said:
This is probably an easy question, but I can't find a function (maybe my
syntax is off...) to search for [a-z] in a string. If someone would
help out, I'd appreciate it!

Also, how would you recommend searching for the following in the same
string:
[a-z]
[A-Z]
[0-9]
-

My approach would be to perform four separte checks, but I'm thinking
there might be some cool approach that would use a dictionary or array.
Ideas?

Are you looking for the actual string [a-z] in a string, or are you
looking for the regular expression [a-z] (any one lowercase letter)

Actual string:
>>> import re
>>> regex = re.compile('\[a-z\]')
>>> regex.search('123123[a-z]adsfasfd').group()
'[a-z]'

Regex pattern:
>>> import re
>>> regex = re.compile('[a-z]')
>>> regex.search('123123123a123123b123123c').group() 'a'
>>> regex.findall('123123123a123123b123123c')
['a', 'b', 'c']

You could also do:
>>> import re
>>> regex = re.compile('[a-z]|[A-Z]')
>>> regex.findall('123a23423b13123c123123A123B123C')
['a', 'b', 'c', 'A', 'B', 'C']

There are probably other ways to do it with list comprehensions, but
does that help?



Jay
 
B

Bob Gailer

This is probably an easy question, but I can't find a function (maybe my
syntax is off...) to search for [a-z] in a string. If someone would help
out, I'd appreciate it!

Also, how would you recommend searching for the following in the same string:
[a-z]
[A-Z]
[0-9]
-

My approach would be to perform four separte checks, but I'm thinking
there might be some cool approach that would use a dictionary or array. Ideas?

Sounds like a perfect application for regular expressions. Check out the re
module. Example:
>>> import re
>>> re.findall(r'[a-zA-Z0-9]*', 'This is fun')
['This', '', 'is', '', 'fun', '']

Bob Gailer
(e-mail address removed)
303 442 2625
 
C

Christopher Koppler

Also, how would I check if [a-z] is not in a string?

import string

sometext = "whatever"
if not string.lowercase in sometext:
said:
Thanks again,
Hank

Hank said:
This is probably an easy question, but I can't find a function (maybe my
syntax is off...) to search for [a-z] in a string. If someone would
help out, I'd appreciate it!

Also, how would you recommend searching for the following in the same
string:
[a-z]

if string.lowercase in sometext:

if string.uppercase in sometext: ...

if string.digits in sometext: ...

As others have shown, you can of course do this with regular
expressions, but the string module is your friend in this case, and
makes for very readable code, which is also unicode-ready, since
string.(lower|upper)case also contain accented and other characters.
Just do a dir(string) to see what other goodies there are...
 
C

Christopher Koppler

which is also unicode-ready, since
string.(lower|upper)case also contain accented and other characters.

Ouch, I take it back (the unicode thing), it's just all the
lower/uppercase letters from the latin-1 set, but not in unicode...

Also, if you want to check for ONLY [a-z], you can check for
string.lowercase[0:26]...



hasty, hasty, hasty...
 
P

Peter Otten

Christopher said:
Also, how would I check if [a-z] is not in a string?

import string

sometext = "whatever"
if not string.lowercase in sometext:
<do what you wanted to do>

Let's see:
.... print "do what you wanted to"
....
do what you wanted to

I doubt that this is what you expected.

s1 in s2

tests if s1 is a substring of s2, but you want

def contains(s, chars):
for c in s:
if c in chars:
return True
return False
expressions, but the string module is your friend in this case, and
makes for very readable code, which is also unicode-ready, since
string.(lower|upper)case also contain accented and other characters.
Just do a dir(string) to see what other goodies there are...

Most of these are already available as methods of the str class and are
duplicated here only for backwards compatibility.

What's actually in string.lowercase/uppercase depends on the locale, you
should by no means take latin-1 for granted.

You have already withdrawn the unicode-ready claim.

Nasty, nasty, nasty :)

Peter
 
C

Christopher Koppler

Christopher said:
Also, how would I check if [a-z] is not in a string?

import string

sometext = "whatever"
if not string.lowercase in sometext:
<do what you wanted to do>

Let's see:
... print "do what you wanted to"
...
do what you wanted to

I doubt that this is what you expected.

s1 in s2

tests if s1 is a substring of s2, but you want

def contains(s, chars):
for c in s:
if c in chars:
return True
return False
expressions, but the string module is your friend in this case, and
makes for very readable code, which is also unicode-ready, since
string.(lower|upper)case also contain accented and other characters.
Just do a dir(string) to see what other goodies there are...

Most of these are already available as methods of the str class and are
duplicated here only for backwards compatibility.

What's actually in string.lowercase/uppercase depends on the locale, you
should by no means take latin-1 for granted.

You have already withdrawn the unicode-ready claim.

Nasty, nasty, nasty :)

Peter

<High embarassement mode>
Big oops. Yeah, that goes to show what happens if you multitask
yourself and don't let your computer do it - I'm preparing for the
second of the two Oracle Certified Associate exams, which I take
tomorrow, and thought I'd take some time out and maybe answer some
questions here... Seems I'm a bit too confused and disorganized to do
that right now...
</HEM>
 
P

Peter Abel

Hank Kingwood said:
Also, how would I check if [a-z] is not in a string?

Thanks again,
Hank

Hank said:
This is probably an easy question, but I can't find a function (maybe my
syntax is off...) to search for [a-z] in a string. If someone would
help out, I'd appreciate it!

Also, how would you recommend searching for the following in the same
string:
[a-z]
[A-Z]
[0-9]
-

My approach would be to perform four separte checks, but I'm thinking
there might be some cool approach that would use a dictionary or array.
Ideas?

Thanks,
Hank

Use regular expressions.
import re
any_string="find a-z, A-Z and 0-9, **+#-#?** no specials"
re_search=re.compile(r'(\w+)')
re_search.findall(any_string) ['find', 'a', 'z', 'A', 'Z', 'and', '0', '9', 'no', 'specials']
# Or
re_search=re.compile(r'(\w)')
re_search.findall(any_string)
['f', 'i', 'n', 'd', 'a', 'z', 'A', 'Z', 'a', 'n', 'd', '0', '9', 'n',
'o', 's', 'p', 'e', 'c', 'i', 'a', 'l', 's']
The **\w** means a-z plus A-Z plus 0-9 plus _.
If you doen't want to get the underline you have to define:
[a-zA-Z0-9] instead.

For more information read the docu for the re-module.

Regards
Peter
 

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

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,440
Latest member
YoungBorel

Latest Threads

Top