DEV Community

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

Posted on • Edited on

endswith in Python

Buy Me a Coffee

*Memo for string, bytes and bytearray functions:

*Memo for a string, bytes and bytearray:

str.endswith() and bytes.endswith() or bytearray.endswith() can check if suffix is the suffix of the string and bytes or bytearray respectively between [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-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.

<String>:

v = 'hello world'

print(v.endswith('ld'))             # True
print(v.endswith('ld', None, None)) # 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', 10)) # False

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

print(v.endswith('lo', 3, 4)) # False
print(v.endswith('lo', 3, 5)) # 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'))                        # 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(bytearray(b'ld'), 9))  # True
print(v.endswith(b'ld', 10))            # False
print(v.endswith(bytearray(b'ld'), 10)) # False

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

print(v.endswith(b'lo', 3, 4))            # False
print(v.endswith(bytearray(b'lo'), 3, 4)) # False
print(v.endswith(b'lo', 3, 5))            # True
print(v.endswith(bytearray(b'lo'), 3, 5)) # 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)