DEV Community

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

Posted on • Edited on

endswith in Python

Buy Me a Coffee

*Memo:

  • My post explains string, bytes and bytearray functions.

str.endswith() and bytes.endswith() or bytearray.endswith() can check if suffix is the suffix of the string and bytes or bytearray respectively in the range [start, end) as shown below:

*Memo:

  • The 1st argument is suffix(Required-Type:str and tuple(str) for str.endswith() or Bytes-like object and tuple(Bytes-like object) for bytes.endswith() and bytearray.endswith()):
    • It's the suffix of zero or more characters and bytes respectively.
    • If it's an empty string and bytes or bytearray respectively, True is returned.
    • Don't use suffix=.
  • 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.
  • start and end can 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.endswith('ld'))             # True
print(v.endswith('ld', None, None)) # True
print(v.endswith('ld', -100, 100))  # True
print(v.endswith('LD'))             # False
print(v.endswith('lo'))             # False

print(v.endswith(('ld', 'lo'))) # True
print(v.endswith(('lo', 'ld'))) # True
print(v.endswith(('lo', 'wo'))) # False

print(v.endswith('ld', 9))  # True
print(v.endswith('ld', -2)) # True
print(v.endswith('ld', 10)) # False
print(v.endswith('ld', -1)) # False

print(v.endswith('ld', 9, 10))  # False
print(v.endswith('ld', -2, -1)) # False
print(v.endswith('ld', 9, 11))  # True
print(v.endswith('ld', -2, 11)) # True

print(v.endswith('lo', 3, 4))   # False
print(v.endswith('lo', -8, -7)) # False
print(v.endswith('lo', 3, 5))   # True
print(v.endswith('lo', -8, -6)) # True

print(v.endswith('')) # True
Enter fullscreen mode Exit fullscreen mode
v = ''

print(v.endswith('')) # True
Enter fullscreen mode Exit fullscreen mode

<Bytes & Bytearray)>:

v = b'hello world'
v = bytearray(b'hello world')

print(v.endswith(b'ld'))                        # True
print(v.endswith(bytearray(b'ld')))             # True
print(v.endswith(b'ld', None, None))            # True
print(v.endswith(bytearray(b'ld'), None, None)) # True
print(v.endswith(b'ld', -100, 100))             # True
print(v.endswith(bytearray(b'ld'), -100, 100))  # True
print(v.endswith(b'LD'))                        # False
print(v.endswith(bytearray(b'LD')))             # False
print(v.endswith(b'lo'))                        # False
print(v.endswith(bytearray(b'lo')))             # False

print(v.endswith((b'ld', b'lo')))                       # True
print(v.endswith((bytearray(b'ld'), bytearray(b'lo')))) # True
print(v.endswith((b'lo', b'ld')))                       # True
print(v.endswith((bytearray(b'lo'), bytearray(b'ld')))) # True
print(v.endswith((b'lo', b'wo')))                       # False
print(v.endswith((bytearray(b'lo'), bytearray(b'wo')))) # False

print(v.endswith(b'ld', 9))             # True
print(v.endswith(b'ld', -2))            # True
print(v.endswith(bytearray(b'ld'), 9))  # True
print(v.endswith(bytearray(b'ld'), -2)) # True
print(v.endswith(b'ld', 10))            # False
print(v.endswith(b'ld', -1))            # False
print(v.endswith(bytearray(b'ld'), 10)) # False
print(v.endswith(bytearray(b'ld'), -1)) # False

print(v.endswith(b'ld', 9, 10))             # False
print(v.endswith(b'ld', -2, -1))            # False
print(v.endswith(bytearray(b'ld'), 9, 10))  # False
print(v.endswith(bytearray(b'ld'), -2, -1)) # False
print(v.endswith(b'ld', 9, 11))             # True
print(v.endswith(b'ld', -2, 11))            # True
print(v.endswith(bytearray(b'ld'), 9, 11))  # True
print(v.endswith(bytearray(b'ld'), -2, 11)) # True

print(v.endswith(b'lo', 3, 4))              # False
print(v.endswith(b'lo', -8, -7))            # False
print(v.endswith(bytearray(b'lo'), 3, 4))   # False
print(v.endswith(bytearray(b'lo'), -8, -7)) # False
print(v.endswith(b'lo', 3, 5))              # True
print(v.endswith(b'lo', -8, -6))            # True
print(v.endswith(bytearray(b'lo'), 3, 5))   # True
print(v.endswith(bytearray(b'lo'), -8, -6)) # True

print(v.endswith(b''))            # True
print(v.endswith(bytearray(b''))) # True
Enter fullscreen mode Exit fullscreen mode
v = b''
v = bytearray(b'')

print(v.endswith(b''))            # True
print(v.endswith(bytearray(b''))) # True
Enter fullscreen mode Exit fullscreen mode

Top comments (0)