[Tutor] finding strings in other strings

Randall Randall wolfkin@freedomspace.net
Sun, 06 Aug 2000 03:54:55 -0400 (EDT)


On 06-Aug-2000 Brett wrote:
>       I was making a little program to rename mp3 files, but didnt want it to 
>  effect other files in the same directory.  I tried using something like 
>  this to have the program only effect mp3 files:
>  
>  files  = os.listdir(os.curdir)
>  for x in files:
>       if 'mp3' in x:
>  
>  After having the program screw up a couple of times I checked out the 
>  documentation and found out that the first term (in my attempt 'mp3') must 
>  have a length of 1 if the second term is a string.  I fixed it in this case 
>  by using <if x[-3:] == 'mp3'>, but i realized this wouldnt work if i wanted 
>  to locate something like this in the middle of some strings, like if some 
>  members of a list had a two or three character long string in them and I 
>  wanted to do stuff only to them.  So anyway, is there a way to determine if 
>  a string is part of a longer string?  Hope you can understand what i'm 
>  talking about, had a little trouble figuring out what I wanted to say.

I believe "if string.find('mp3', x) != -1:" will do
what you want. 


-- 
Randall (wolfkin@freedomspace.net).
Crypto key: www.freedomspace.net/~wolfkin/crypto.text
On a visible but distant shore, a new image of man;
The shape of his own future, now in his own hands.-- Johnny Clegg.