DEV Community

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

Posted on • Edited on

strip in Python

Buy Me a Coffee

*Memo for string, bytes and bytearray functions:

*Memo for a string, bytes and bytearray:

str.strip() and bytes.strip() or bytearray.strip() can remove zero or more characters and bytes from the left and right 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.strip(), Bytes-like object for bytes.strip() and bytearray.strip() or NoneType):
    • It's the zero or more characters and bytes to remove from the left and right 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 and suffix respectively.
    • If it's not set or None, " " is set.
    • Don't use chars=.

<String>:

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

print('"' + v.strip() + '"')
print('"' + v.strip(" ") + '"')
print('"' + v.strip(" ABC ") + '"')
# "aa bb cc"

print('"' + v.strip("a ") + '"')
print('"' + v.strip(" a") + '"')
# "bb cc"

print('"' + v.strip("c ") + '"')
print('"' + v.strip(" c") + '"')
# "aa bb"

print('"' + v.strip("ac ") + '"')
print('"' + v.strip(" ac") + '"')
print('"' + v.strip("a c") + '"')
# "bb"

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

<Bytes & Bytearray>:

bytes:

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

print(v.strip())
print(v.strip(b" "))
print(v.strip(bytearray(b" ")))
print(v.strip(b" ABC "))
print(v.strip(bytearray(b" ABC ")))
# b'aa bb cc'

print(v.strip(b"a "))
print(v.strip(bytearray(b"a ")))
print(v.strip(b" a"))
print(v.strip(bytearray(b" a")))
# b'bb cc'

print(v.strip(b"c "))
print(v.strip(bytearray(b"c ")))
print(v.strip(b" c"))
print(v.strip(bytearray(b" c")))
# b'aa bb'

print(v.strip(b"ac "))
print(v.strip(bytearray(b"ac ")))
print(v.strip(b" ac"))
print(v.strip(bytearray(b" ac")))
print(v.strip(b"a c"))
print(v.strip(bytearray(b"a c")))
# b'bb'

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

bytearray:

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

print(v.strip())
print(v.strip(b" "))
print(v.strip(bytearray(b" ")))
print(v.strip(b" ABC "))
print(v.strip(bytearray(b" ABC ")))
# bytearray(b'aa bb cc')

print(v.strip(b"a "))
print(v.strip(bytearray(b"a ")))
print(v.strip(b" a"))
print(v.strip(bytearray(b" a")))
# bytearray(b'bb cc')

print(v.strip(b"c "))
print(v.strip(bytearray(b"c ")))
print(v.strip(b" c"))
print(v.strip(bytearray(b" c")))
# bytearray(b'aa bb')

print(v.strip(b"ac "))
print(v.strip(bytearray(b"ac ")))
print(v.strip(b" ac"))
print(v.strip(bytearray(b" ac")))
print(v.strip(b"a c"))
print(v.strip(bytearray(b"a c")))
# bytearray(b'bb')

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

Top comments (0)