DEV Community

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

Posted on • Edited on

lstrip in Python

Buy Me a Coffee

*Memo for string, bytes and bytearray functions:

*Memo for a string, bytes and bytearray:

str.lstrip() and bytes.lstrip() or bytearray.lstrip() can remove zero or more characters and bytes from the left side of the string and bytes or bytearray one by one respectively as shown below:

*Memo:

  • The 1st argument is chars(Optional-Defualt:None-Type:str for str.lstrip(), Bytes-like object for bytes.lstrip() and bytearray.lstrip() or NoneType):
    • It's the zero or more characters and bytes to remove from the left side of the string and bytes or bytearray one by one respectively.
    • Its each character and byte are considered one by one so it's not a prefix respectively.
    • If it's not set or None, " " is set.
    • Don't use chars=.

<String>:

v = "  aa bb cc  "
   # ↑↑        ↑↑

print('"' + v.lstrip() + '"')
print('"' + v.lstrip(" ") + '"')
print('"' + v.lstrip("c ") + '"')
print('"' + v.lstrip(" c") + '"')
print('"' + v.lstrip(" ABC ") + '"')
# "aa bb cc  "
#          ↑↑

print('"' + v.lstrip("a ") + '"')
print('"' + v.lstrip(" a") + '"')
print('"' + v.lstrip("ac ") + '"')
print('"' + v.lstrip(" ac") + '"')
print('"' + v.lstrip("a c") + '"')
# "bb cc  "
#       ↑↑

print('"' + v.lstrip("") + '"')
print('"' + v.lstrip("a") + '"')
print('"' + v.lstrip("c") + '"')
print('"' + v.lstrip("ac") + '"')
# "  aa bb cc  "
#  ↑↑        ↑↑
Enter fullscreen mode Exit fullscreen mode

<Bytes & Bytearray>:

bytes:

v = b"  aa bb cc  "
#     ↑↑        ↑↑

print(v.lstrip())
print(v.lstrip(b" "))
print(v.lstrip(bytearray(b" ")))
print(v.lstrip(b"c "))
print(v.lstrip(bytearray(b"c ")))
print(v.lstrip(b" c"))
print(v.lstrip(bytearray(b" c")))
print(v.lstrip(b" ABC "))
print(v.lstrip(bytearray(b" ABC ")))
# b'aa bb cc  '
#           ↑↑

print(v.lstrip(b"a "))
print(v.lstrip(bytearray(b"a ")))
print(v.lstrip(b" a"))
print(v.lstrip(bytearray(b" a")))
print(v.lstrip(b"ac "))
print(v.lstrip(bytearray(b"ac ")))
print(v.lstrip(b" ac"))
print(v.lstrip(bytearray(b" ac")))
print(v.lstrip(b"a c"))
print(v.lstrip(bytearray(b"a c")))
# b'bb cc  '
#        ↑↑

print(v.lstrip(b""))
print(v.lstrip(bytearray(b"")))
print(v.lstrip(b"a"))
print(v.lstrip(bytearray(b"a")))
print(v.lstrip(b"c"))
print(v.lstrip(bytearray(b"c")))
print(v.lstrip(b"ac"))
print(v.lstrip(bytearray(b"ac")))
# b'  aa bb cc  '
#   ↑↑        ↑↑
Enter fullscreen mode Exit fullscreen mode

bytearray:

v = bytearray(b"  aa bb cc  ")
              # ↑↑        ↑↑

print(v.lstrip())
print(v.lstrip(b" "))
print(v.lstrip(bytearray(b" ")))
print(v.lstrip(b"c "))
print(v.lstrip(bytearray(b"c ")))
print(v.lstrip(b" c"))
print(v.lstrip(bytearray(b" c")))
print(v.lstrip(b" ABC "))
print(v.lstrip(bytearray(b" ABC ")))
# bytearray(b'aa bb cc  ')
#                     ↑↑

print(v.lstrip(b"a "))
print(v.lstrip(bytearray(b"a ")))
print(v.lstrip(b" a"))
print(v.lstrip(bytearray(b" a")))
print(v.lstrip(b"ac "))
print(v.lstrip(bytearray(b"ac ")))
print(v.lstrip(b" ac"))
print(v.lstrip(bytearray(b" ac")))
print(v.lstrip(b"a c"))
print(v.lstrip(bytearray(b"a c")))
# bytearray(b'bb cc  ')
#                  ↑↑

print(v.lstrip(b""))
print(v.lstrip(bytearray(b"")))
print(v.lstrip(b"a"))
print(v.lstrip(bytearray(b"a")))
print(v.lstrip(b"c"))
print(v.lstrip(bytearray(b"c")))
print(v.lstrip(b"ac"))
print(v.lstrip(bytearray(b"ac")))
# bytearray(b'  aa bb cc  ')
#             ↑↑        ↑↑
Enter fullscreen mode Exit fullscreen mode

Top comments (0)