DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

rfind in Python

Buy Me a Coffee

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

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

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

*Memo:

  • The 1st argument is sub(Required-Type:str for str.rfind() or Bytes-like object and int for bytes.rfind() and bytearray.rfind()):
    • 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.
  • -1 is returned if sub doesn't exist.

<String>:

v = 'grapes peach'

print(v.rfind('pe'))             # 7
print(v.rfind('pe', None, None)) # 7
print(v.rfind('PE'))             # -1
print(v.rfind('pe', 3))          # 7
print(v.rfind('pe', 4))          # 7
print(v.rfind('pe', 7))          # 7
print(v.rfind('pe', 8))          # -1
print(v.rfind('pe', 0, 4))       # -1
print(v.rfind('pe', 0, 5))       # 3
print(v.rfind('pe', 7, 12))      # 7
print(v.rfind('pe', 8, 12))      # -1

print(v.rfind(''))          # 12
print(v.rfind('g', 0, 1))   # 0
print(v.rfind('h', 11, 12)) # 11
print(v.rfind('abc'))       # -1
Enter fullscreen mode Exit fullscreen mode
v = ''

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

<Byte String(bytes & bytearray)>:

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

print(v.rfind(b'pe'))                        # 7
print(v.rfind(bytearray(b'pe'))              # 7
print(v.rfind(b'pe', None, None))            # 7
print(v.rfind(bytearray(b'pe'), None, None)) # 7
print(v.rfind(b'PE'))                        # -1
print(v.rfind(bytearray(b'PE')))             # -1
print(v.rfind(b'pe', 3))                     # 7
print(v.rfind(bytearray(b'pe'), 3))          # 7
print(v.rfind(b'pe', 4))                     # 7
print(v.rfind(bytearray(b'pe'), 4))          # 7
print(v.rfind(b'pe', 7))                     # 7
print(v.rfind(bytearray(b'pe'), 7))          # 7
print(v.rfind(b'pe', 8))                     # -1
print(v.rfind(bytearray(b'pe'), 8))          # -1
print(v.rfind(b'pe', 0, 4))                  # -1
print(v.rfind(bytearray(b'pe'), 0, 4))       # -1
print(v.rfind(b'pe', 0, 5))                  # 3
print(v.rfind(bytearray(b'pe'), 0, 5))       # 3
print(v.rfind(b'pe', 7, 12))                 # 7
print(v.rfind(bytearray(b'pe'), 7, 12))      # 7
print(v.rfind(b'pe', 8, 12))                 # -1
print(v.rfind(bytearray(b'pe'), 8, 12))      # -1

print(v.rfind(b''))                     # 12
print(v.rfind(bytearray(b'')))          # 12
print(v.rfind(b'g', 0, 1))              # 0
print(v.rfind(ord('g'), 0, 1))          # 0
print(v.rfind(103, 0, 1))               # 0
print(v.rfind(bytearray(b'g')))         # 0
print(v.rfind(b'h', 11, 12))            # 11
print(v.rfind(ord('h'), 11, 12))        # 11
print(v.rfind(104, 11, 12))             # 11
print(v.rfind(bytearray(b'h'), 11, 12)) # 11
print(v.rfind(b'abc'))                  # -1
print(v.rfind(bytearray(b'abc')))       # -1
Enter fullscreen mode Exit fullscreen mode
v = b''
v = bytearray(b'')

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

Top comments (0)