-4

I have been trying to create the str.find method a function for a lesson I am doing and I am not quite sure how it is done. If anyone can help me please do. I am running on python 2.7 if that helps. I am trying to find a subsequence in a larger sequence and how many times it appears in the sequence. ex. ACAAGATGCCATTGTCCCCCGGCCTCCTGCTGCTGCTGCTCTCCGGGGCCACGGCCACCGCTGCCCTGCCCCTGGAGGGTGGCCCCACCGGCCGAGACAGCGAGCATATGCAGGAAGCGGCAGGAATAAGGAAAAGCAGCCTCCTGACTTTCCTCGCTTGGTGGTTTGAGTGGACCTCCCAGGCCAGTGCCGGGCCCCTCATAGGAGAGGAAGCTCGGGAGGTGGCCAGGCGGCAGGAAGGCGCACCCCCCCAGCAATCCGCGCGCCGGGACAGAATGCCCTGCAGGAACTTCTTCTGGAAGACCTTCTCCTCCTGCAAATAAAACCTCACCCATGAATGCTCACGCAAGTTTAATTACAGACCTGAA

and now I must find where each occurrence of TTG is in commas. again I really appreciate the help

  • Are you looking to extend string so that it has a find method or are you trying just to find something in a string? Also, possible duplicate of http://stackoverflow.com/questions/4664850/find-all-occurrences-of-a-substring-in-python – Los Frijoles Nov 04 '12 at 23:30
  • As a hint, look at `startswith` function (http://docs.python.org/2/library/stdtypes.html) – Yevgen Yampolskiy Nov 04 '12 at 23:33
  • def find(x): return x.find() ^^ is what i was hoping it would be i need to use a function that can act as the str.find method to help me find all the places a substring is found in the original more lengthy string. sorry I was not so clear – Kevin Arias Nov 04 '12 at 23:43

2 Answers2

4

Methods in Python can be turned into ordinary functions by accessing the method through the class.

>>> 'abc'.find('b')
1
>>> str.find('abc', 'b')
1
Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
  • would this be able to work if i need to find a substring in a string and how many times it appears? – Kevin Arias Nov 05 '12 at 00:16
  • Read the [Python Documentation](http://docs.python.org/2/library/stdtypes.html#string-methods). You can repeatedly call the function with a different offset. – Dietrich Epp Nov 05 '12 at 00:32
1

Python's string.find method is implemented by the function fastsearch in stringlib/fastsearch.h. This function implements a version of the well known Boyer–Moore algorithm.

Gareth Rees
  • 64,967
  • 9
  • 133
  • 163