How can I search a string for any occurrence of substring (including partial matches) in python?How can I represent an 'Enum' in Python?How can I safely create a nested directory?How can I remove a trailing newline?How do I parse a string to a float or int?How can I make a time delay in Python?How can you profile a Python script?How can I count the occurrences of a list item?Does Python have a string 'contains' substring method?How do I lowercase a string in Python?How to check for partial anagram in Python 3

What next step can I take in solving this sudoku?

Should you only use colons and periods in dialogues?

In what state are satellites left in when they are left in a graveyard orbit?

The Planck constant for mathematicians

I was promised a work PC but still awaiting approval 3 months later so using my own laptop - Is it fair to ask employer for laptop insurance?

What are examples of experiments or studies where pre-registration would not be feasible?

How are unbalanced coaxial cables used for broadcasting TV signals without any problems?

What 68-pin connector is this on my 2.5" solid state drive?

How to publish superseding results without creating enemies

Is there a tool to measure the "maturity" of a code in Git?

Is the Dodge action perceptible to other characters?

Make 1998 using the least possible digits 8

Can I conceal an antihero's insanity - and should I?

Parallel resistance in electric circuits

In a hashmap, the addition of a new element to the internal linked list of a bucket is always at the end. Why?

How major are these paintwork & rust problems?

Asked to Not Use Transactions and to Use A Workaround to Simulate One

Interaction between Teferi Time Raveler and Enduring Ideal

POSIX compatible way to get user name associated with a user ID

If I want an interpretable model, are there methods other than Linear Regression?

How do EVA suits manage water excretion?

Is there any reason to concentrate on the Thunderous Smite spell after using its effects?

How to write characters doing illogical things in a believable way?

2000s space film where an alien species has almost wiped out the human race in a war



How can I search a string for any occurrence of substring (including partial matches) in python?


How can I represent an 'Enum' in Python?How can I safely create a nested directory?How can I remove a trailing newline?How do I parse a string to a float or int?How can I make a time delay in Python?How can you profile a Python script?How can I count the occurrences of a list item?Does Python have a string 'contains' substring method?How do I lowercase a string in Python?How to check for partial anagram in Python 3






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I am trying to write a program which will search through a series of strings and return true if any part of a substring is found.



For example, say the substring I am interested in is:



GATCGATC


The program should return True for:



GGTGGATCGATC


And should also return true for (because it ends with GATC):



GGTGTTTTGATC


So far I have:



def matchpat(str1, str2):
'''Find a pattern in a string'''
if str1 in str2:
return True
else:
return False


This function works, but only if the whole pattern is present, it will return False for partial matches.










share|improve this question



















  • 2





    Partial matches with any threshold in particular? Because a matching letter is also a partial match

    – yatu
    Mar 28 at 10:49











  • Please clarify your task: "any part of a substring is found" - how big the part should be? Does one letter counts as part of the string? Or do we look at the fact that "GATCGATC" is repeating of "GATC" substring?

    – andnik
    Mar 28 at 10:55











  • Please post a sample for both True and False cases.

    – DirtyBit
    Mar 28 at 10:57






  • 1





    So the sentence This is not working should also return TRUE because it ends with a substring of GATCGATC? (The sentence contains the letter G, which is a substring of GATCGATC)

    – Dominique
    Mar 28 at 11:01











  • Yes, if the string ends with a partial match, even a single G, then it should return True.

    – umbro_tracksuit
    Mar 28 at 11:10

















1















I am trying to write a program which will search through a series of strings and return true if any part of a substring is found.



For example, say the substring I am interested in is:



GATCGATC


The program should return True for:



GGTGGATCGATC


And should also return true for (because it ends with GATC):



GGTGTTTTGATC


So far I have:



def matchpat(str1, str2):
'''Find a pattern in a string'''
if str1 in str2:
return True
else:
return False


This function works, but only if the whole pattern is present, it will return False for partial matches.










share|improve this question



















  • 2





    Partial matches with any threshold in particular? Because a matching letter is also a partial match

    – yatu
    Mar 28 at 10:49











  • Please clarify your task: "any part of a substring is found" - how big the part should be? Does one letter counts as part of the string? Or do we look at the fact that "GATCGATC" is repeating of "GATC" substring?

    – andnik
    Mar 28 at 10:55











  • Please post a sample for both True and False cases.

    – DirtyBit
    Mar 28 at 10:57






  • 1





    So the sentence This is not working should also return TRUE because it ends with a substring of GATCGATC? (The sentence contains the letter G, which is a substring of GATCGATC)

    – Dominique
    Mar 28 at 11:01











  • Yes, if the string ends with a partial match, even a single G, then it should return True.

    – umbro_tracksuit
    Mar 28 at 11:10













1












1








1








I am trying to write a program which will search through a series of strings and return true if any part of a substring is found.



For example, say the substring I am interested in is:



GATCGATC


The program should return True for:



GGTGGATCGATC


And should also return true for (because it ends with GATC):



GGTGTTTTGATC


So far I have:



def matchpat(str1, str2):
'''Find a pattern in a string'''
if str1 in str2:
return True
else:
return False


This function works, but only if the whole pattern is present, it will return False for partial matches.










share|improve this question














I am trying to write a program which will search through a series of strings and return true if any part of a substring is found.



For example, say the substring I am interested in is:



GATCGATC


The program should return True for:



GGTGGATCGATC


And should also return true for (because it ends with GATC):



GGTGTTTTGATC


So far I have:



def matchpat(str1, str2):
'''Find a pattern in a string'''
if str1 in str2:
return True
else:
return False


This function works, but only if the whole pattern is present, it will return False for partial matches.







python python-3.x






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 28 at 10:47









umbro_tracksuitumbro_tracksuit

265 bronze badges




265 bronze badges










  • 2





    Partial matches with any threshold in particular? Because a matching letter is also a partial match

    – yatu
    Mar 28 at 10:49











  • Please clarify your task: "any part of a substring is found" - how big the part should be? Does one letter counts as part of the string? Or do we look at the fact that "GATCGATC" is repeating of "GATC" substring?

    – andnik
    Mar 28 at 10:55











  • Please post a sample for both True and False cases.

    – DirtyBit
    Mar 28 at 10:57






  • 1





    So the sentence This is not working should also return TRUE because it ends with a substring of GATCGATC? (The sentence contains the letter G, which is a substring of GATCGATC)

    – Dominique
    Mar 28 at 11:01











  • Yes, if the string ends with a partial match, even a single G, then it should return True.

    – umbro_tracksuit
    Mar 28 at 11:10












  • 2





    Partial matches with any threshold in particular? Because a matching letter is also a partial match

    – yatu
    Mar 28 at 10:49











  • Please clarify your task: "any part of a substring is found" - how big the part should be? Does one letter counts as part of the string? Or do we look at the fact that "GATCGATC" is repeating of "GATC" substring?

    – andnik
    Mar 28 at 10:55











  • Please post a sample for both True and False cases.

    – DirtyBit
    Mar 28 at 10:57






  • 1





    So the sentence This is not working should also return TRUE because it ends with a substring of GATCGATC? (The sentence contains the letter G, which is a substring of GATCGATC)

    – Dominique
    Mar 28 at 11:01











  • Yes, if the string ends with a partial match, even a single G, then it should return True.

    – umbro_tracksuit
    Mar 28 at 11:10







2




2





Partial matches with any threshold in particular? Because a matching letter is also a partial match

– yatu
Mar 28 at 10:49





Partial matches with any threshold in particular? Because a matching letter is also a partial match

– yatu
Mar 28 at 10:49













Please clarify your task: "any part of a substring is found" - how big the part should be? Does one letter counts as part of the string? Or do we look at the fact that "GATCGATC" is repeating of "GATC" substring?

– andnik
Mar 28 at 10:55





Please clarify your task: "any part of a substring is found" - how big the part should be? Does one letter counts as part of the string? Or do we look at the fact that "GATCGATC" is repeating of "GATC" substring?

– andnik
Mar 28 at 10:55













Please post a sample for both True and False cases.

– DirtyBit
Mar 28 at 10:57





Please post a sample for both True and False cases.

– DirtyBit
Mar 28 at 10:57




1




1





So the sentence This is not working should also return TRUE because it ends with a substring of GATCGATC? (The sentence contains the letter G, which is a substring of GATCGATC)

– Dominique
Mar 28 at 11:01





So the sentence This is not working should also return TRUE because it ends with a substring of GATCGATC? (The sentence contains the letter G, which is a substring of GATCGATC)

– Dominique
Mar 28 at 11:01













Yes, if the string ends with a partial match, even a single G, then it should return True.

– umbro_tracksuit
Mar 28 at 11:10





Yes, if the string ends with a partial match, even a single G, then it should return True.

– umbro_tracksuit
Mar 28 at 11:10












3 Answers
3






active

oldest

votes


















2
















Hi i made this code which works.
You can change it to be more dynamical with the variables



text = 'GGTGGATCGATC'
lookingFor = 'GATCGATC'

def method():
print('in check')
if lookingFor in text:
return true
else:
return false

def main():
method()
if __name__ == "__main__":


If you want to make the method take in input you can pass it in the method definition:



def method(text, lookingFor)





share|improve this answer



























  • A lot of typos in the code, but it works :-). def method():, True, False, indents below def

    – pyano
    Apr 12 at 17:11











  • Haha sorry and thanks!

    – Phillip Eismark
    Apr 18 at 13:36


















0
















I have used a library called Fuzzywuzzy for a similar problem, which worked well for my requirements and may help.



It uses Levenshtein distance metric to compare the strings.






share|improve this answer

























  • Thank you for your suggestion, but I wanted to try and write the code without importing any modules.

    – umbro_tracksuit
    Mar 28 at 11:10


















0
















You can use re module to do that



import re

patterntomatch = "GATCGATC"
patterntomatch = "[0]".format(patterntomatch)

TextTomatch = "This is something"

matchObj = re.match(patterntomatch,TextTomatch,re.I)

if matchObj:
print ("match found")
else:
print("no match found")





share|improve this answer



























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );














    draft saved

    draft discarded
















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55395670%2fhow-can-i-search-a-string-for-any-occurrence-of-substring-including-partial-mat%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2
















    Hi i made this code which works.
    You can change it to be more dynamical with the variables



    text = 'GGTGGATCGATC'
    lookingFor = 'GATCGATC'

    def method():
    print('in check')
    if lookingFor in text:
    return true
    else:
    return false

    def main():
    method()
    if __name__ == "__main__":


    If you want to make the method take in input you can pass it in the method definition:



    def method(text, lookingFor)





    share|improve this answer



























    • A lot of typos in the code, but it works :-). def method():, True, False, indents below def

      – pyano
      Apr 12 at 17:11











    • Haha sorry and thanks!

      – Phillip Eismark
      Apr 18 at 13:36















    2
















    Hi i made this code which works.
    You can change it to be more dynamical with the variables



    text = 'GGTGGATCGATC'
    lookingFor = 'GATCGATC'

    def method():
    print('in check')
    if lookingFor in text:
    return true
    else:
    return false

    def main():
    method()
    if __name__ == "__main__":


    If you want to make the method take in input you can pass it in the method definition:



    def method(text, lookingFor)





    share|improve this answer



























    • A lot of typos in the code, but it works :-). def method():, True, False, indents below def

      – pyano
      Apr 12 at 17:11











    • Haha sorry and thanks!

      – Phillip Eismark
      Apr 18 at 13:36













    2














    2










    2









    Hi i made this code which works.
    You can change it to be more dynamical with the variables



    text = 'GGTGGATCGATC'
    lookingFor = 'GATCGATC'

    def method():
    print('in check')
    if lookingFor in text:
    return true
    else:
    return false

    def main():
    method()
    if __name__ == "__main__":


    If you want to make the method take in input you can pass it in the method definition:



    def method(text, lookingFor)





    share|improve this answer















    Hi i made this code which works.
    You can change it to be more dynamical with the variables



    text = 'GGTGGATCGATC'
    lookingFor = 'GATCGATC'

    def method():
    print('in check')
    if lookingFor in text:
    return true
    else:
    return false

    def main():
    method()
    if __name__ == "__main__":


    If you want to make the method take in input you can pass it in the method definition:



    def method(text, lookingFor)






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Apr 18 at 13:36

























    answered Mar 28 at 11:17









    Phillip EismarkPhillip Eismark

    214 bronze badges




    214 bronze badges















    • A lot of typos in the code, but it works :-). def method():, True, False, indents below def

      – pyano
      Apr 12 at 17:11











    • Haha sorry and thanks!

      – Phillip Eismark
      Apr 18 at 13:36

















    • A lot of typos in the code, but it works :-). def method():, True, False, indents below def

      – pyano
      Apr 12 at 17:11











    • Haha sorry and thanks!

      – Phillip Eismark
      Apr 18 at 13:36
















    A lot of typos in the code, but it works :-). def method():, True, False, indents below def

    – pyano
    Apr 12 at 17:11





    A lot of typos in the code, but it works :-). def method():, True, False, indents below def

    – pyano
    Apr 12 at 17:11













    Haha sorry and thanks!

    – Phillip Eismark
    Apr 18 at 13:36





    Haha sorry and thanks!

    – Phillip Eismark
    Apr 18 at 13:36













    0
















    I have used a library called Fuzzywuzzy for a similar problem, which worked well for my requirements and may help.



    It uses Levenshtein distance metric to compare the strings.






    share|improve this answer

























    • Thank you for your suggestion, but I wanted to try and write the code without importing any modules.

      – umbro_tracksuit
      Mar 28 at 11:10















    0
















    I have used a library called Fuzzywuzzy for a similar problem, which worked well for my requirements and may help.



    It uses Levenshtein distance metric to compare the strings.






    share|improve this answer

























    • Thank you for your suggestion, but I wanted to try and write the code without importing any modules.

      – umbro_tracksuit
      Mar 28 at 11:10













    0














    0










    0









    I have used a library called Fuzzywuzzy for a similar problem, which worked well for my requirements and may help.



    It uses Levenshtein distance metric to compare the strings.






    share|improve this answer













    I have used a library called Fuzzywuzzy for a similar problem, which worked well for my requirements and may help.



    It uses Levenshtein distance metric to compare the strings.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Mar 28 at 10:55









    NickNick

    1,5232 gold badges18 silver badges37 bronze badges




    1,5232 gold badges18 silver badges37 bronze badges















    • Thank you for your suggestion, but I wanted to try and write the code without importing any modules.

      – umbro_tracksuit
      Mar 28 at 11:10

















    • Thank you for your suggestion, but I wanted to try and write the code without importing any modules.

      – umbro_tracksuit
      Mar 28 at 11:10
















    Thank you for your suggestion, but I wanted to try and write the code without importing any modules.

    – umbro_tracksuit
    Mar 28 at 11:10





    Thank you for your suggestion, but I wanted to try and write the code without importing any modules.

    – umbro_tracksuit
    Mar 28 at 11:10











    0
















    You can use re module to do that



    import re

    patterntomatch = "GATCGATC"
    patterntomatch = "[0]".format(patterntomatch)

    TextTomatch = "This is something"

    matchObj = re.match(patterntomatch,TextTomatch,re.I)

    if matchObj:
    print ("match found")
    else:
    print("no match found")





    share|improve this answer





























      0
















      You can use re module to do that



      import re

      patterntomatch = "GATCGATC"
      patterntomatch = "[0]".format(patterntomatch)

      TextTomatch = "This is something"

      matchObj = re.match(patterntomatch,TextTomatch,re.I)

      if matchObj:
      print ("match found")
      else:
      print("no match found")





      share|improve this answer



























        0














        0










        0









        You can use re module to do that



        import re

        patterntomatch = "GATCGATC"
        patterntomatch = "[0]".format(patterntomatch)

        TextTomatch = "This is something"

        matchObj = re.match(patterntomatch,TextTomatch,re.I)

        if matchObj:
        print ("match found")
        else:
        print("no match found")





        share|improve this answer













        You can use re module to do that



        import re

        patterntomatch = "GATCGATC"
        patterntomatch = "[0]".format(patterntomatch)

        TextTomatch = "This is something"

        matchObj = re.match(patterntomatch,TextTomatch,re.I)

        if matchObj:
        print ("match found")
        else:
        print("no match found")






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 28 at 11:40









        Venkataraman RVenkataraman R

        2,45111 silver badges20 bronze badges




        2,45111 silver badges20 bronze badges































            draft saved

            draft discarded















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55395670%2fhow-can-i-search-a-string-for-any-occurrence-of-substring-including-partial-mat%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Obelisk of Theodosius Contents History Description Notes Bibliography Further reading External links Navigation menuAge of spirituality : late antique and early Christian art, third to seventh centuryOver 60 picturesObelisks of the World41°00′21.24″N 28°58′31.43″E / 41.0059000°N 28.9753972°E / 41.0059000; 28.97539727724550-7235741376235741376

            밀양 대씨 역사 각주 함께 보기 둘러보기 메뉴밀양 대씨

            1973년 목차 사건 문화 탄생 사망 노벨상 달력 둘러보기 메뉴