[Tutor] Finding all the letters in a string?

Eric Lake ericlake at ubuntu.com
Tue Sep 18 01:41:49 CEST 2007


On Mon, Sep 17, 2007 at 07:21:09PM -0400, Andrew Nelsen wrote:
> 
>    I was wondering, recently, the most expedient way to take a string
>    with [@#$%^&*] and alpha-numeric characters [ie.
>    "^@%#*$@*$g@)$&^@&^$F"] and place all of the letters in a string or
>    list. I thought there could be obvious ways:
>    A) Find all the letters, put them in a list, one by one. Something
>    like (I'm not sure yet how I'd do it...):
>    import string
>    list = {}
>    string = "@*&^$&*^@$g*(&@$*(&$@c(*&*(&c^&%&^%"
>    for x in string:
>        if x <is in string.letters?>
>            list = list + [x]
>    B) Delete all the characters in the string that don't match
>    string.letters:
>    No idea...strip()?
>    Thanks,
>    Drew

> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor

This is what I came up with for the first part of the question.

#!/usr/bin/env python
# -*- coding: iso-8859-15 -*-
import string

lst = []
chars = '@*&^$&*^@$g*(&@$*(&$@c(*&*(&c^&%&^%'
for x in chars:
    if x in string.ascii_letters:
        lst.append(x)

for n in lst:
    print n,


I am sure that there is probably a better way though.
-- 

Thanks
Eric Lake
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 481 bytes
Desc: Digital signature
Url : http://mail.python.org/pipermail/tutor/attachments/20070917/52b78a54/attachment-0001.pgp 


More information about the Tutor mailing list