*Memo:
- My post explains string, bytes and bytearray functions.
str.count() and bytes.count() or bytearray.count() can respectively count the substrings and byte substrings matched to sub in the range [start, end) as shown below:
*Memo:
- The 1st argument is
sub(Required-Type:strforstr.count()or Bytes-like object andintforbytes.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-Default:None-Type:int/NoneType):- It's a start index(inclusive).
- If it's
None, the 1st index is set to it. - Don't use
start.
- The 3rd argument is
end(Optional-Default:None-Type:int/NoneType):- It's an end index(exclusive).
- If it's
None, the string and bytes or bytearray length are set to it respectively. - Don't use
end.
- The string and bytes or bytearray length
+ 1are returned at a maximum ifsubis an empty string and bytes or bytearray respectively. -
startandendcan be signed indices(zero and positive and negative indices). - Error doesn't occur even if
[start, end)is out of the range[The 1st index, The string and bytes or bytearray length).
<String>:
v = 'hello world'
print(v.count('l'))
print(v.count('l', None, None))
print(v.count('l', -100, 100))
# 3
print(v.count('o'))
# 2
print(v.count('d'))
# 1
print(v.count('l', 3))
print(v.count('l', -8))
print(v.count('l', 3, 11))
print(v.count('l', -8, 11))
# 2
print(v.count('l', 3, 9))
print(v.count('l', -8, -2))
# 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))
print(v.count('', -11, 11))
# 12
print(v.count('', 0, 4))
print(v.count('', -11, -7))
print(v.count('', 3, 7))
print(v.count('', -8, -4))
print(v.count('', 7, 11))
print(v.count('', -4, 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(bytearray(b'l')))
print(v.count(ord('l')))
print(v.count(108))
print(v.count(b'l', None, None))
print(v.count(bytearray(b'l'), None, None))
print(v.count(ord('l'), None, None))
print(v.count(108, None, None))
print(v.count(b'l', -100, 100))
print(v.count(bytearray(b'l'), -100, 100))
print(v.count(ord('l'), -100, 100))
print(v.count(108, -100, 100))
# 3
print(v.count(b'o'))
print(v.count(bytearray(b'o')))
print(v.count(ord('o')))
print(v.count(111))
# 2
print(v.count(b'd'))
print(v.count(bytearray(b'd')))
print(v.count(ord('d')))
print(v.count(100))
# 1
print(v.count(b'l', 3))
print(v.count(bytearray(b'l'), 3))
print(v.count(ord('l'), 3))
print(v.count(108, 3))
print(v.count(b'l', -8))
print(v.count(bytearray(b'l'), -8))
print(v.count(ord('l'), -8))
print(v.count(108, -8))
print(v.count(b'l', 3, 11))
print(v.count(bytearray(b'l'), 3, 11))
print(v.count(ord('l'), 3, 11))
print(v.count(108, 3, 11))
print(v.count(b'l', -8, 11))
print(v.count(bytearray(b'l'), -8, 11))
print(v.count(ord('l'), -8, 11))
print(v.count(108, -8, 11))
# 2
print(v.count(b'l', 3, 9))
print(v.count(bytearray(b'l'), 3, 9))
print(v.count(ord('l'), 3, 9))
print(v.count(108, 3, 9))
print(v.count(b'l', -8, -2))
print(v.count(bytearray(b'l'), -8, -2))
print(v.count(ord('l'), -8, -2))
print(v.count(108, -8, -2))
# 1
print(v.count(b'L'))
print(v.count(bytearray(b'L')))
print(v.count(ord('L')))
print(v.count(76))
print(v.count(b'a'))
print(v.count(bytearray(b'a')))
print(v.count(ord('a')))
print(v.count(97))
# 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))
print(v.count(b'', -11, 11))
print(v.count(bytearray(b''), -11, 11))
# 12
print(v.count(b'', 0, 4))
print(v.count(bytearray(b''), 0, 4))
print(v.count(b'', -11, -7))
print(v.count(bytearray(b''), -11, -7))
print(v.count(b'', 3, 7))
print(v.count(bytearray(b''), 3, 7))
print(v.count(b'', -8, -4))
print(v.count(bytearray(b''), -8, -4))
print(v.count(b'', 7, 11))
print(v.count(bytearray(b''), 7, 11))
print(v.count(b'', -4, 11))
print(v.count(bytearray(b''), -4, 11))
# 5
v = b''
v = bytearray(b'')
print(v.count(b''))
print(v.count(bytearray(b'')))
# 1
Top comments (0)