*Memo for string, bytes and bytearray functions:
- My post explains index().
- My post explains rindex().
- My post explains find().
- My post explains rfind().
- My post explains ord(), sorted() and reversed().
*Memo for a string, bytes and bytearray:
str.count() and bytes.count() or bytearray.count() can count the substrings and byte substrings respectively matched to sub
between [start, end)
as shown below:
*Memo:
- The 1st argument is
sub
(Required-Type:str
forstr.count()
or Bytes-like object andint
forbytes.count()
andbytearray.count()
):- It's the substring and byte substring of zero or more characters and bytes respectively.
- Don't use
sub=
.
- The 2nd argument is
start
(Optional-Type:int
or NoneType):- It's a start index(inclusive).
- If it's not set or
None
,0
is set to it. - Don't use
start
.
- The 3rd argument is
end
(Optional-Type:int
or NoneType):- It's an end index(exclusive).
- If it's not set or
None
, the length+ 1
is set to it. - Don't use
end
.
- The length
+ 1
is returned ifsub
is an empty string and bytes or bytearray respectively and ifstart
andend
aren't set.
<String>:
v = 'hello world' print(v.count('l')) print(v.count('l', None, None)) # 3 print(v.count('o')) # 2 print(v.count('d')) # 1 print(v.count('l', 3)) print(v.count('l', 3, 11)) # 2 print(v.count('l', 3, 9)) # 1 print(v.count('L')) print(v.count('a')) # 0 print(v.count('ll')) print(v.count('lo')) print(v.count('wo')) # 1 print(v.count('')) print(v.count('', 0, 11)) # 12 print(v.count('', 0, 4)) print(v.count('', 3, 7)) print(v.count('', 7, 11)) # 5
v = '' print(v.count('')) # 1
<Bytes & Bytearray>:
v = b'hello world' v = bytearray(b'hello world') print(v.count(b'l')) print(v.count(ord('l'))) print(v.count(108)) print(v.count(bytearray(b'l'))) print(v.count(b'l', None, None)) print(v.count(ord('l'), None, None)) print(v.count(108, None, None)) print(v.count(bytearray(b'l'), None, None)) # 3 print(v.count(b'o')) print(v.count(ord('o'))) print(v.count(111)) print(v.count(bytearray(b'o'))) # 2 print(v.count(b'd')) print(v.count(ord('d'))) print(v.count(100)) print(v.count(bytearray(b'd'))) # 1 print(v.count(b'l', 3)) print(v.count(ord('l'), 3)) print(v.count(108, 3)) print(v.count(bytearray(b'l'), 3)) print(v.count(b'l', 3, 11)) print(v.count(ord('l'), 3, 11)) print(v.count(108, 3, 11)) print(v.count(bytearray(b'l'), 3, 11)) # 2 print(v.count(b'l', 3, 9)) print(v.count(ord('l'), 3, 9)) print(v.count(108, 3, 9)) print(v.count(bytearray(b'l'), 3, 9)) # 1 print(v.count(b'L')) print(v.count(ord('L'))) print(v.count(76)) print(v.count(bytearray(b'L'))) print(v.count(b'a')) print(v.count(ord('a'))) print(v.count(97)) print(v.count(bytearray(b'a'))) # 0 print(v.count(b'll')) print(v.count(bytearray(b'll'))) print(v.count(b'lo')) print(v.count(bytearray(b'lo'))) print(v.count(b'wo')) print(v.count(bytearray(b'wo'))) # 1 print(v.count(b'')) print(v.count(bytearray(b''))) print(v.count(b'', 0, 11)) print(v.count(bytearray(b''), 0, 11)) # 12 print(v.count(b'', 0, 4)) print(v.count(bytearray(b''), 0, 4)) print(v.count(b'', 3, 7)) print(v.count(bytearray(b''), 3, 7)) print(v.count(b'', 7, 11)) print(v.count(bytearray(b''), 7, 11)) # 5
v = b'' v = bytearray(b'') print(v.count(b'')) print(v.count(bytearray(b''))) # 1
Top comments (0)