Array of Chars to String

rbt rbt at athop1.ath.vt.edu
Tue Apr 19 16:50:24 EDT 2005


James Stroud wrote:
> Hello,
> 
> I am looking for a nice way to take only those charachters from a string that 
> are in another string and make a new string:
> 
> 
>>>>astr = "Bob Carol Ted Alice"
>>>>letters = "adB"
>>>>some_func(astr,letters)
> 
> "Bad"

astr = "Bob Carol Ted Alice"
letters = "adB"
both = [x for x in astr if x in letters]
print both

 >>>
['B', 'a', 'd']
 >>>



More information about the Python-list mailing list