extracting a substring

Kent Johnson kent at kentsjohnson.com
Wed Apr 19 08:36:57 EDT 2006


b83503104 at yahoo.com wrote:
> Hi,
> I have a bunch of strings like
> a53bc_531.txt
> a53bc_2285.txt
> ...
> a53bc_359.txt
> 
> and I want to extract the numbers 531, 2285, ...,359.
> 
> One thing for sure is that these numbers are the ONLY part that is
> changing; all the other characters are always fixed.

In that case a fixed slice will do what you want:

In [1]: s='a53bc_531.txt'

In [2]: s[6:-4]
Out[2]: '531'

Kent



More information about the Python-list mailing list