Python string count not working properly? [duplicate] - python

This question already has answers here:
How can I find the number of overlapping sequences in a String with Python? [duplicate]
(4 answers)
Closed 6 years ago.
There are two occurrences of 'aba' in 'ababa' (0th index and 2nd index):
myString = 'ababa'
print(myString.count('aba'))
Yet this code outputs a value of: 1
I know this issue seems really simple, but shouldn't the answer be 2 here?
If not, then isn't the count function not really doing what it's supposed to?
Is there a simple alternative?

From the Python string function documentation
Return the number of non-overlapping occurrences of substring sub in the range [start, end]. Optional arguments start and end are interpreted as in slice notation.
count does not count overlapping occurrences.
If you want to count overlapping occurrences you can use regex with a lookahead assertion:
import re
print(len(re.findall('(?=aba)', 'ababa')))

Documentation to the rescue: https://docs.python.org/2/library/string.html
Return the number of (non-overlapping) occurrences of substring sub in string s[start:end]. Defaults for start and end and interpretation of negative values are the same as for slices.

Related

How count() function works? [duplicate]

This question already has answers here:
Count number of occurrences of a substring in a string
(36 answers)
Closed 24 days ago.
Given:
st = "banana"
print(st.count("ana"))
# => 1
How come the count is 1, it should be 2 right? please explain to me.
As per documentation of str.count():
Return the number of non-overlapping occurrences of substring sub ...
In your case, the occurrences of "ana" are overlapping.

Reversing a string including the first letter in python [duplicate]

This question already has answers here:
Why does reversing a list using slice notation with 0 as "stop" not return the entire list?
(9 answers)
How do I reverse a string in Python?
(19 answers)
Closed 10 days ago.
I am trying to reverse a string in python but i cannot include the first letter.
I tried this code:
a = "Helloworld"
print(a[3:0:-1])
but it doesn't work.
I also tried:
a = "Helloworld"
print(a[3:-1:-1])
It displays nothing when i try this.
The code you tried doesn't work because the slice a[3:0:-1] starts at index 3 and goes all the way to index 0 (in reverse), but it includes index 0, which is the first letter of the string.
The slice a[3:-1:-1] starts at index 3 and goes to the index before the last one (-1), but in reverse. This would give an empty string because the step value of -1 goes in the opposite direction of the start and end indices.
To reverse the string excluding the first letter, you can slice it like this:
a = "Helloworld"
print(a[1:][::-1])

Is str.count() wrong? [duplicate]

This question already has answers here:
String count with overlapping occurrences [closed]
(25 answers)
How String.count() works? [duplicate]
(2 answers)
Python string count not working properly? [duplicate]
(2 answers)
Closed 4 years ago.
I'm having fun with some challenges and one of them makes me count substrings in a string. I have a problem specifically with "banana":
str = "banana"
print(str.count("ana"))
This should return 2 because "ana" appears two times:
b a n a n a
a n a
a n a
But str.count("ana") returns only 1. I've also tried with regexp:
import re
str = "banana"
print(len(re.findall("ana", str)))
But it also returns 1. Am I missing something?
thank you!
Yes, you are missing something.
str.count(): Return the number of (non-overlapping) occurrences of substring sub in string s

How String.count() works? [duplicate]

This question already has answers here:
Count overlapping substring in a string [duplicate]
(6 answers)
Closed 6 years ago.
I am new to python and learning. As given here count() method when used on strings gives the number of occurrences of sub string in a string.
So when i Do :
'BANANA'.count('ANA')
Expected output should be 2 as 'ANA' occurs twice in 'BANANA' but count returns 1.
Can someone please explain this, or maybe i have misunderstood something.
Please point me in the right direction.
>>> help(str.count)
Help on method_descriptor:
count(...)
S.count(sub[, start[, end]]) -> int
Return the number of non-overlapping occurrences of substring sub in
string S[start:end]. Optional arguments start and end are
interpreted as in slice notation.
Notice the non-overlapping.
You can use regular expressions to find it. Use the function findall from module re to find overlapping occurences
import re
len(re.findall('(?=ANA)', 'BANANA'))
which yields 2.
Or yields 3 here:
import re
len(re.findall('(?=ANA)', 'BANANAANA'))

Is there a Python idiom for the last n elements of a sequence? [duplicate]

This question already has answers here:
How to get last items of a list in Python?
(5 answers)
Python - How to extract the last x elements from a list [duplicate]
(4 answers)
Closed 9 years ago.
e.g., for a sequence of unknown length, what is the most "Pythonic" way of getting the last n elements?
Obviously I could calculate the starting and ending indices. Is there anything slicker?
Yes, by using negative indices:
last_five = somesequence[-5:]
Negative indices in a slice are relative to the sequence length.
Use negative indexing.
seq[-1] is the last element of a sequence. seq[-3:] gives you the last three.
Try:
sequence[-n:]
(text to make Stack Overflow happy)

Categories