index in Python
Super Kai (Kazuya Ito)

Super Kai (Kazuya Ito) @hyperkai

About: I'm a web developer. Buy Me a Coffee: ko-fi.com/superkai SO: stackoverflow.com/users/3247006/super-kai-kazuya-ito X(Twitter): twitter.com/superkai_kazuya FB: facebook.com/superkai.kazuya

Joined:
Oct 21, 2021

index in Python

Publish Date: Aug 3
0 0

Buy Me a Coffee

*Memos for a string and byte string(bytes and bytearray) functions:

*Memos for a string and byte string(bytes and bytearray):

str.index() and bytes.index() or bytearray.index() can get the 1st index of the substring and byte substring respectively matched to sub between [start, end), searching from the left to the right (with error if the substring and byte substring don't exist respectively) as shown below:

*Memos:

  • The 1st argument is sub(Required-Type:str for str.index() or Bytes-like object and int for bytes.index() and bytearray.index()):
    • 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.
    • If it's not set or None, 0 is set.
    • Don't use start.
  • The 3rd argument is end(Optional-Type:int or NoneType):
    • It's an end index.
    • If it's not set or None, the length + 1 is set.
    • Don't use end.
  • Error occurs if the substring and byte substring don't exist respectively.

<String>:

v = 'grapes peach'

print(v.index('pe'))             # 3
print(v.index('pe', None, None)) # 3
print(v.index('pe', 3))          # 3
print(v.index('pe', 4))          # 7
print(v.index('pe', 7))          # 7
print(v.index('pe', 0, 5))       # 3
print(v.index('pe', 7, 12))      # 7

print(v.index(''))          # 0
print(v.index('g', 0, 1))   # 0
print(v.index('h', 11, 12)) # 11

print(v.index('PE'))
print(v.index('pe', 8))
print(v.index('pe', 0, 4))
print(v.index('pe', 8, 12))
print(v.index('abc'))
# ValueError: substring not found
Enter fullscreen mode Exit fullscreen mode
v = ''

print(v.index('')) # 0
Enter fullscreen mode Exit fullscreen mode

<Byte String(bytes & bytearray)>:

v = b'grapes peach'
v = bytearray('grapes peach'.encode())

print(v.index(b'pe'))                        # 3
print(v.index(bytearray(b'pe')))             # 3
print(v.index(b'pe', None, None))            # 3
print(v.index(bytearray(b'pe'), None, None)) # 3
print(v.index(b'pe', 3))                     # 3
print(v.index(bytearray(b'pe'), 3))          # 3
print(v.index(b'pe', 4))                     # 7
print(v.index(bytearray(b'pe'), 4))          # 7
print(v.index(b'pe', 7))                     # 7
print(v.index(bytearray(b'pe'), 7))          # 7
print(v.index(b'pe', 0, 5))                  # 3
print(v.index(bytearray(b'pe'), 0, 5))       # 3
print(v.index(b'pe', 7, 12))                 # 11
print(v.index(bytearray(b'pe'), 7, 12))      # 11

print(v.index(b''))                     # 0
print(v.index(bytearray(b'')))          # 0
print(v.index(b'g', 0, 1))              # 0
print(v.index(ord('g'), 0, 1))          # 0
print(v.index(103, 0, 1))               # 0
print(v.index(bytearray(b'g'), 0, 1))   # 0
print(v.index(b'h', 11, 12))            # 11
print(v.index(ord('h'), 11, 12))        # 11
print(v.index(104, 11, 12))             # 11
print(v.index(bytearray(b'h'), 11, 12)) # 11

print(v.index(b'PE'))
print(v.index(bytearray(b'PE')))
print(v.index(b'pe', 8))
print(v.index(bytearray(b'pe'), 8))
print(v.index(b'pe', 0, 4))
print(v.index(bytearray(b'pe'), 0, 4))
print(v.index(b'pe', 8, 12))
print(v.index(bytearray(b'pe'), 8, 12))
print(v.index(b'abc'))
print(v.index(bytearray(b'abc')))
# ValueError: subsection not found
Enter fullscreen mode Exit fullscreen mode
v = b''
v = bytearray(b'')

print(v.index(b''))            # 0
print(v.index(bytearray(b''))) # 0
Enter fullscreen mode Exit fullscreen mode

Comments 0 total

    Add comment