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
Related
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.
This question already has answers here:
Count the number of occurrences of a character in a string
(26 answers)
Closed 10 months ago.
eg: "ABC.sample.int.int01" like that, only want to check this string contains 3 dots.
Thanks
You can check it like this, with count attribute of str in python. If it is equal to 3, then your strings actually contains exactly 3 dots.
if 'ABC.sample.int.int01'.count('.') == 3:
...
else:
...
This question already has answers here:
String count with overlapping occurrences [closed]
(25 answers)
Closed 1 year ago.
I tried to create a program which returns the number of times a certain string occurs in the main string.
main_string="ABCDCDC"
find_string="CDC"
print(main_string.count(find_string))
Output=1
....
But there are 2 CDC. Is there any other ways to solve?
Try using regex:
print(len(re.findall(fr"(?={find_string})", main_string)))
Or try using this list comprehension:
x = len(find_string)
print(len([main_string[i:x + i] for i in range(len(main_string)) if main_string[i:x + i] == find_string]))
Both codes output:
2
This question already has answers here:
How to extract numbers from a string in Python?
(19 answers)
extract digits in a simple way from a python string [duplicate]
(5 answers)
Closed 3 years ago.
I have the following string:
string_a = 81^A55
from which I'm trying to get the following list
string_a_int = [81,55]
I'm able to split the string into a list made by numbers as follows
list_only_number = re.split('[A-Z]+', string_a)
list_only_numbers = [81^, 55]
but I'm trying to figure out how to skip also the ^.
Any help would be much appreciated.
This question already has answers here:
Why does map return a map object instead of a list in Python 3?
(4 answers)
Getting a map() to return a list in Python 3.x
(11 answers)
Closed 5 years ago.
How to Convert a string like '123' to int 1,2 and 3 so that I can perform 1+2+3. I am new to python. Can you please help me? I am not able to split the list. I don't think splitting the string will be of any use as there are no delimiters. Can you help me to understand how can this string elements be separated and treated as intergers?
x = "123"
s = 0
for a in x:
s = int(a) + s