Python 3: finding a set of letters in a string using loops
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
add a comment |
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
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
add a comment |
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
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
python
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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)
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
add a comment |
just do the following
mystery_string.count('cat')
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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)
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
add a comment |
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)
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
add a comment |
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)
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)
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
add a comment |
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
add a comment |
just do the following
mystery_string.count('cat')
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
add a comment |
just do the following
mystery_string.count('cat')
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
add a comment |
just do the following
mystery_string.count('cat')
just do the following
mystery_string.count('cat')
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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