[Tutor] Splitting text
Dave Kuhlman
dkuhlman at rexx.com
Thu Jun 29 23:29:10 CEST 2006
On Thu, Jun 29, 2006 at 01:06:54PM -0700, Matthew White wrote:
> Hello Appu,
>
> You can use the count() method to find the number of occurances of a
> substring within a string:
>
> >>> a = 'TCCCTGCGGCGCATGAGTGACTGGCGTATTTAGCCCGTCACATTTA'
> >>> a.count('ATTTA')
> 2
>
And, if you need to search for a more complicated pattern,
consider using the regular expression module (re):
>>> import re
>>> a = 'TCCCTGCGGCGCATGAGTGACTGGCGTATTTAGCCCGTCACATTTA'
>>> pat = re.compile('ATTTA')
>>> pat.findall(a)
['ATTTA', 'ATTTA']
>>> len(pat.findall(a))
2
Dave
[snip]
--
Dave Kuhlman
http://www.rexx.com/~dkuhlman
More information about the Tutor
mailing list