DEV Community

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

Posted on • Edited on

rstrip in Python

Buy Me a Coffee

*Memo for a string and byte string(bytes and bytearray) functions:

*Memo for a string and byte string(bytes and bytearray):

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

*Memo:

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

<String>:

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

print('"' + v.rstrip() + '"')
print('"' + v.rstrip(" ") + '"')
print('"' + v.rstrip("a ") + '"')
print('"' + v.rstrip(" a") + '"')
print('"' + v.rstrip(" ABC ") + '"')
# "  aa bb cc"
#  ↑↑

print('"' + v.rstrip("c ") + '"')
print('"' + v.rstrip(" c") + '"')
print('"' + v.rstrip("ac ") + '"')
print('"' + v.rstrip(" ac") + '"')
print('"' + v.rstrip("a c") + '"')
# "  aa bb"
#  ↑↑

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

<Byte String>:

bytes:

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

print(v.rstrip())
print(v.rstrip(b" "))
print(v.rstrip(bytearray(b" ")))
print(v.rstrip(b"a "))
print(v.rstrip(bytearray(b"a ")))
print(v.rstrip(b" a"))
print(v.rstrip(bytearray(b" a")))
print(v.rstrip(b" ABC "))
print(v.rstrip(bytearray(b" ABC ")))
# b'  aa bb cc'
#   ↑↑

print(v.rstrip(b"c "))
print(v.rstrip(bytearray(b"c ")))
print(v.rstrip(b" c"))
print(v.rstrip(bytearray(b" c")))
print(v.rstrip(b"ac "))
print(v.rstrip(bytearray(b"ac ")))
print(v.rstrip(b" ac"))
print(v.rstrip(bytearray(b" ac")))
print(v.rstrip(b"a c"))
print(v.rstrip(bytearray(b"a c")))
# b'  aa bb'
#   ↑↑

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

bytearray:

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

print(v.rstrip())
print(v.rstrip(b" "))
print(v.rstrip(bytearray(b" ")))
print(v.rstrip(b"a "))
print(v.rstrip(bytearray(b"a ")))
print(v.rstrip(b" a"))
print(v.rstrip(bytearray(b" a")))
print(v.rstrip(b" ABC "))
print(v.rstrip(bytearray(b" ABC ")))
# bytearray(b'  aa bb cc')
#             ↑↑

print(v.rstrip(b"c "))
print(v.rstrip(bytearray(b"c ")))
print(v.rstrip(b" c"))
print(v.rstrip(bytearray(b" c")))
print(v.rstrip(b"ac "))
print(v.rstrip(bytearray(b"ac ")))
print(v.rstrip(b" ac"))
print(v.rstrip(bytearray(b" ac")))
print(v.rstrip(b"a c"))
print(v.rstrip(bytearray(b"a c")))
# bytearray(b'  aa bb')
#             ↑↑

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

Top comments (0)