endswith 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

endswith in Python

Publish Date: Aug 4
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.endswith() and bytes.endswith() or bytearray.endswith() can check if suffix is the suffix of a string and byte string respectively as shown below:

*Memos:

  • 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.
    • If it's an empty string or byte string, True is returned.
    • Don't use suffix=.
  • 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.

<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

<Byte String(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

Comments 0 total

    Add comment