1

I'm trying to enumerate the time the program finds a specific string in a longer text but I'm stuck, the program shows the length of the response string instead of the times ultrias is present in response. Here's the code:

ultrias = "am 17"
response = "Hi i am 17, did you know I am 17"
num = 0
for ultrias in response:
    num += 1
print (num)
1
  • 2
    Do you need to count overlapping occurrences ? Say how many time is 171 in 17171 ? One or two ? Commented Feb 14, 2015 at 12:15

5 Answers 5

2

Others have answered with a few different ways to achieve your requirement, however, none explained why the output of your code is the length of the input string.

for loops have a loop variable that is assigned values from the object that is being iterated over. In the case of the object being a string, the for loop will iterate in order over each character in the string, assigning the character to the loop variable e.g.

>>> for i in "Hi there":
...     print i
... 
H
i

t
h
e
r
e

So you can see that i is assigned in turn a character from the string "Hi there". The same thing is happening in your code: ultrias is the loop variable and is being assigned consecutive characters from the string "Hi i am 17, did you know I am 17".

ultrias = "am 17"
response = "Hi i am 17, did you know I am 17"
num = 0
for ultrias in response:
    print(ultrias)
    num += 1
print (num)

The output will be:

H
i

i

a
m

1
7
.
.
.

3

Also note that the value of ultrias after the loop has terminated is the last value assigned it by the loop - not "am 17" but "7".

Sign up to request clarification or add additional context in comments.

Comments

1

Python strings have a built-in str.count() method that can do that:

ultrias = "am 17"
response = "Hi i am 17, did you know I am 17"
print(response.count(ultrias))

output

2

If you aren't permitted to use the str.count() method, you could do something like this:

ultrias = "am 17"
response = "Hi i am 17, did you know I am 17"

count = 0
data = response[:]
while True:
    start = data.find(ultrias)
    if start < 0:
        break
    data = data[start + len(ultrias):]
    count += 1
print(count)

But I wouldn't recommend it.

Neither of these algorithms handle overlapping matches, but the second example can be easily modified to do that:

count = 0
data = response[:]
while True:
    start = data.find(ultrias)
    if start < 0:
        break
    data = data[start + 1:]
    count += 1
print(count)

Comments

1

You can use str.count:

>>> ultrias = "am 17"
>>> response = "Hi i am 17, did you know I am 17"
>>> response.count(ultrias)
2

Comments

1

Use string.count(string) to get what you need.

>>> find = "am 17"
>>> string = "Hi I am 17, did you know I am 17"
>>> string.count(find)
2

Comments

1

Through re module.

>>> ultrias = "am 17"
>>> response = "Hi i am 17, did you know I am 17"
>>> print(len(re.findall(ultrias, response)))
2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.