*Memo for string, bytes and bytearray functions:
*Memo for a 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 bytes or bytearray one by one respectively as shown below:
*Memo:
- The 1st argument is
chars
(Optional-Defualt:None
-Type:str
forstr.rstrip()
, Bytes-like object forbytes.rstrip()
andbytearray.rstrip()
or NoneType):- It's the zero or more characters and bytes to remove from the 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 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 " # ↑↑ ↑↑
<Bytes & Bytearray>:
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 ' # ↑↑ ↑↑
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 ') # ↑↑ ↑↑
Top comments (0)