How String.count() works? [duplicate] - python

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'))

Related

How to return the first half of a string? [duplicate]

This question already has answers here:
Understanding slicing
(38 answers)
Closed 2 years ago.
Assuming the string has at least one character in it. If the string length is odd, then you may assume it returns (𝑛−1)/2 characters where n represents how many characters are in the original string.
For example:
'small' => 'sm'
I would also like to write another function that returns the 2nd half of a string.
You can just do an integer division (//) to get the integer value of the division to get the desired (n-1)/2 value for odd n. Therefore, having the following:
>>> my_string = "small"
>>> print(my_string[:len(my_string)//2])
sm
You could do the similar thing with using math.floor to be more explicit, but result is the same.

In String Slicing str [:3] , in case the string is less than length 3, will this slicing return or not? [duplicate]

This question already has answers here:
Understanding slicing
(38 answers)
Closed 3 years ago.
If I have a string of variable length and I want to return only first 3 characters of the string.
str[:3]
this works but I want to know if the string is of lesser length, suppose 2, "ab" or just "a", will this slicing work for that too?
Thanks in advance
Python will return at most :n characters:
'a'[:3] will simply return 'a'. ''[:3] returns ''.
You could have tested for yourself in less time than it would have taken to open your browser.
But yes.

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

Python string count not working properly? [duplicate]

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.

How do you reverse the letters in a string? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
reverse a string in Python
I'm trying to understand how to reverse the letters in a string. Let's say that I have hello and am looking for the output olleh how would I implement this using the list as a tool?
Using slice notation,
forwards = "hello"
backwards = forwards[::-1]
(The third section of slice notation is the step; in this case, -1 makes it step backwards through the entirety of the string, effectively reversing it.)
or, using the reversed() function:
backwards = ''.join(reversed(forwards))
(Note that without the ''.join(), you'd get a <reversed object at 0x1215a10> instead.)
>>> print backwards
olleh
With slice notation:
string = "Hello!"
reversed_string = string[::-1]

Categories