strip 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

strip 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.strip() and bytes.strip() or bytearray.strip() can remove zero or more characters(chars) from the left and right character of a string and byte string respectively one by one as shown below:

*Memos:

  • 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 to remove from the left and right character of a string or byte string one by one.
    • Its each character is considered one by one so it's not a prefix and suffix.
    • 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

<Byte String>:

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

Comments 0 total

    Add comment