Python 3: finding a set of letters in a string using loops












0















i'm using python 3 and i would like to know how to count the amount of times a set of 3 letters comes up in a sentence:



con = ""
count = 0
for i in mystery_string:
if i == "c":
con = "c"
if i == "a" and con == "c":
con = "ca"
if i == "t" and con == "ca":
con = "cat"
if con == "cat":
count +=1
con = ""
print (count)


this is the code i have so far, however it doesn't seem to work for every case
can someone help me



the thing i also need to explain is that i cannot use the builtin function count()









share|improve this question




















  • 1





    Can you also provide a sample input and expected output?

    – slider
    Nov 24 '18 at 18:09






  • 5





    Possible duplicate of Count the number occurrences of a character in a string

    – Muzol
    Nov 24 '18 at 18:13
















0















i'm using python 3 and i would like to know how to count the amount of times a set of 3 letters comes up in a sentence:



con = ""
count = 0
for i in mystery_string:
if i == "c":
con = "c"
if i == "a" and con == "c":
con = "ca"
if i == "t" and con == "ca":
con = "cat"
if con == "cat":
count +=1
con = ""
print (count)


this is the code i have so far, however it doesn't seem to work for every case
can someone help me



the thing i also need to explain is that i cannot use the builtin function count()









share|improve this question




















  • 1





    Can you also provide a sample input and expected output?

    – slider
    Nov 24 '18 at 18:09






  • 5





    Possible duplicate of Count the number occurrences of a character in a string

    – Muzol
    Nov 24 '18 at 18:13














0












0








0


0






i'm using python 3 and i would like to know how to count the amount of times a set of 3 letters comes up in a sentence:



con = ""
count = 0
for i in mystery_string:
if i == "c":
con = "c"
if i == "a" and con == "c":
con = "ca"
if i == "t" and con == "ca":
con = "cat"
if con == "cat":
count +=1
con = ""
print (count)


this is the code i have so far, however it doesn't seem to work for every case
can someone help me



the thing i also need to explain is that i cannot use the builtin function count()









share|improve this question
















i'm using python 3 and i would like to know how to count the amount of times a set of 3 letters comes up in a sentence:



con = ""
count = 0
for i in mystery_string:
if i == "c":
con = "c"
if i == "a" and con == "c":
con = "ca"
if i == "t" and con == "ca":
con = "cat"
if con == "cat":
count +=1
con = ""
print (count)


this is the code i have so far, however it doesn't seem to work for every case
can someone help me



the thing i also need to explain is that i cannot use the builtin function count()






python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 18:28







Theo Boston

















asked Nov 24 '18 at 18:05









Theo BostonTheo Boston

115




115








  • 1





    Can you also provide a sample input and expected output?

    – slider
    Nov 24 '18 at 18:09






  • 5





    Possible duplicate of Count the number occurrences of a character in a string

    – Muzol
    Nov 24 '18 at 18:13














  • 1





    Can you also provide a sample input and expected output?

    – slider
    Nov 24 '18 at 18:09






  • 5





    Possible duplicate of Count the number occurrences of a character in a string

    – Muzol
    Nov 24 '18 at 18:13








1




1





Can you also provide a sample input and expected output?

– slider
Nov 24 '18 at 18:09





Can you also provide a sample input and expected output?

– slider
Nov 24 '18 at 18:09




5




5





Possible duplicate of Count the number occurrences of a character in a string

– Muzol
Nov 24 '18 at 18:13





Possible duplicate of Count the number occurrences of a character in a string

– Muzol
Nov 24 '18 at 18:13












2 Answers
2






active

oldest

votes


















1














you can use slicing:



word='cat'
count = 0
for i in range(len(mystery_string)-len(word)+1):
if mystery_string[i:i+len(word)]==word:
count +=1
print (count)





share|improve this answer
























  • this solution supports overlapping, contrary to count() function, for example the word 'pop' will be counted once in the string 'popop' if you use the count function

    – prophet-five
    Nov 24 '18 at 18:17











  • Thanks @slider, I guess some people think they know better

    – prophet-five
    Nov 24 '18 at 18:36



















2














just do the following



mystery_string.count('cat')





share|improve this answer



















  • 1





    It is a good solution for this specific word, however the count() method does not support overlapping

    – prophet-five
    Nov 24 '18 at 18:38






  • 1





    you mean examples like if I am counting 'aba' in 'gtfababacd' ?

    – Biswadip Mandal
    Nov 24 '18 at 18:41











  • yes, exactly, this will return 1 instead of 2.

    – prophet-five
    Nov 24 '18 at 18:44











  • cool. sounds about right. Thanks for mentioning

    – Biswadip Mandal
    Nov 24 '18 at 18:45











  • sure, np, I recently encountered this problem myself :)

    – prophet-five
    Nov 24 '18 at 18:47











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/3.0/"u003ecc by-sa 3.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%2f53460993%2fpython-3-finding-a-set-of-letters-in-a-string-using-loops%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














you can use slicing:



word='cat'
count = 0
for i in range(len(mystery_string)-len(word)+1):
if mystery_string[i:i+len(word)]==word:
count +=1
print (count)





share|improve this answer
























  • this solution supports overlapping, contrary to count() function, for example the word 'pop' will be counted once in the string 'popop' if you use the count function

    – prophet-five
    Nov 24 '18 at 18:17











  • Thanks @slider, I guess some people think they know better

    – prophet-five
    Nov 24 '18 at 18:36
















1














you can use slicing:



word='cat'
count = 0
for i in range(len(mystery_string)-len(word)+1):
if mystery_string[i:i+len(word)]==word:
count +=1
print (count)





share|improve this answer
























  • this solution supports overlapping, contrary to count() function, for example the word 'pop' will be counted once in the string 'popop' if you use the count function

    – prophet-five
    Nov 24 '18 at 18:17











  • Thanks @slider, I guess some people think they know better

    – prophet-five
    Nov 24 '18 at 18:36














1












1








1







you can use slicing:



word='cat'
count = 0
for i in range(len(mystery_string)-len(word)+1):
if mystery_string[i:i+len(word)]==word:
count +=1
print (count)





share|improve this answer













you can use slicing:



word='cat'
count = 0
for i in range(len(mystery_string)-len(word)+1):
if mystery_string[i:i+len(word)]==word:
count +=1
print (count)






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 24 '18 at 18:14









prophet-fiveprophet-five

1089




1089













  • this solution supports overlapping, contrary to count() function, for example the word 'pop' will be counted once in the string 'popop' if you use the count function

    – prophet-five
    Nov 24 '18 at 18:17











  • Thanks @slider, I guess some people think they know better

    – prophet-five
    Nov 24 '18 at 18:36



















  • this solution supports overlapping, contrary to count() function, for example the word 'pop' will be counted once in the string 'popop' if you use the count function

    – prophet-five
    Nov 24 '18 at 18:17











  • Thanks @slider, I guess some people think they know better

    – prophet-five
    Nov 24 '18 at 18:36

















this solution supports overlapping, contrary to count() function, for example the word 'pop' will be counted once in the string 'popop' if you use the count function

– prophet-five
Nov 24 '18 at 18:17





this solution supports overlapping, contrary to count() function, for example the word 'pop' will be counted once in the string 'popop' if you use the count function

– prophet-five
Nov 24 '18 at 18:17













Thanks @slider, I guess some people think they know better

– prophet-five
Nov 24 '18 at 18:36





Thanks @slider, I guess some people think they know better

– prophet-five
Nov 24 '18 at 18:36













2














just do the following



mystery_string.count('cat')





share|improve this answer



















  • 1





    It is a good solution for this specific word, however the count() method does not support overlapping

    – prophet-five
    Nov 24 '18 at 18:38






  • 1





    you mean examples like if I am counting 'aba' in 'gtfababacd' ?

    – Biswadip Mandal
    Nov 24 '18 at 18:41











  • yes, exactly, this will return 1 instead of 2.

    – prophet-five
    Nov 24 '18 at 18:44











  • cool. sounds about right. Thanks for mentioning

    – Biswadip Mandal
    Nov 24 '18 at 18:45











  • sure, np, I recently encountered this problem myself :)

    – prophet-five
    Nov 24 '18 at 18:47
















2














just do the following



mystery_string.count('cat')





share|improve this answer



















  • 1





    It is a good solution for this specific word, however the count() method does not support overlapping

    – prophet-five
    Nov 24 '18 at 18:38






  • 1





    you mean examples like if I am counting 'aba' in 'gtfababacd' ?

    – Biswadip Mandal
    Nov 24 '18 at 18:41











  • yes, exactly, this will return 1 instead of 2.

    – prophet-five
    Nov 24 '18 at 18:44











  • cool. sounds about right. Thanks for mentioning

    – Biswadip Mandal
    Nov 24 '18 at 18:45











  • sure, np, I recently encountered this problem myself :)

    – prophet-five
    Nov 24 '18 at 18:47














2












2








2







just do the following



mystery_string.count('cat')





share|improve this answer













just do the following



mystery_string.count('cat')






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 24 '18 at 18:10









Biswadip MandalBiswadip Mandal

1809




1809








  • 1





    It is a good solution for this specific word, however the count() method does not support overlapping

    – prophet-five
    Nov 24 '18 at 18:38






  • 1





    you mean examples like if I am counting 'aba' in 'gtfababacd' ?

    – Biswadip Mandal
    Nov 24 '18 at 18:41











  • yes, exactly, this will return 1 instead of 2.

    – prophet-five
    Nov 24 '18 at 18:44











  • cool. sounds about right. Thanks for mentioning

    – Biswadip Mandal
    Nov 24 '18 at 18:45











  • sure, np, I recently encountered this problem myself :)

    – prophet-five
    Nov 24 '18 at 18:47














  • 1





    It is a good solution for this specific word, however the count() method does not support overlapping

    – prophet-five
    Nov 24 '18 at 18:38






  • 1





    you mean examples like if I am counting 'aba' in 'gtfababacd' ?

    – Biswadip Mandal
    Nov 24 '18 at 18:41











  • yes, exactly, this will return 1 instead of 2.

    – prophet-five
    Nov 24 '18 at 18:44











  • cool. sounds about right. Thanks for mentioning

    – Biswadip Mandal
    Nov 24 '18 at 18:45











  • sure, np, I recently encountered this problem myself :)

    – prophet-five
    Nov 24 '18 at 18:47








1




1





It is a good solution for this specific word, however the count() method does not support overlapping

– prophet-five
Nov 24 '18 at 18:38





It is a good solution for this specific word, however the count() method does not support overlapping

– prophet-five
Nov 24 '18 at 18:38




1




1





you mean examples like if I am counting 'aba' in 'gtfababacd' ?

– Biswadip Mandal
Nov 24 '18 at 18:41





you mean examples like if I am counting 'aba' in 'gtfababacd' ?

– Biswadip Mandal
Nov 24 '18 at 18:41













yes, exactly, this will return 1 instead of 2.

– prophet-five
Nov 24 '18 at 18:44





yes, exactly, this will return 1 instead of 2.

– prophet-five
Nov 24 '18 at 18:44













cool. sounds about right. Thanks for mentioning

– Biswadip Mandal
Nov 24 '18 at 18:45





cool. sounds about right. Thanks for mentioning

– Biswadip Mandal
Nov 24 '18 at 18:45













sure, np, I recently encountered this problem myself :)

– prophet-five
Nov 24 '18 at 18:47





sure, np, I recently encountered this problem myself :)

– prophet-five
Nov 24 '18 at 18:47


















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%2f53460993%2fpython-3-finding-a-set-of-letters-in-a-string-using-loops%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

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'