*Memo for string, bytes and bytearray functions:
- My post explains split().
- My post explains rsplit().
- My post explains splitlines().
- My post explains partition().
- My post explains join().
*Memo for a string, bytes and bytearray:
str.rpartition() and bytes.rpartition() or bytearray.rpartition() can split the string and bytes or bytearray respectively at the 1st occurrence of a separator, searching from the right to the left as shown below:
*Memo:
- The 1st argument is
sep
(Required-Type:str
forstr.rpartition()
or Bytes-like object forbytes.rpartition()
andbytearray.rpartition()
):- It's the separator of the one or more characters and bytes to separate a string and bytes or bytearray respectively.
- An empty string and bytes or bytearray cannot be set respectively.
- Don't use
sep=
.
- It returns a tuple of 3 elements.
- If
sep
isn't found, a tuple of two empty strings and the original string in order and two empty bytes or bytearray and the original bytes or bytearray in order are returned as 3 elements respectively.
<String>:
v = "It's very very good" print(v.rpartition('er')) # ("It's very v", 'er', 'y good') print(v.rpartition('very')) # ("It's very ", 'very', ' good') # ↓ print(v.rpartition(' ')) # ("It's very very", ' ', 'good') # ↓↓ print(v.rpartition(' ')) print(v.rpartition('ER')) print(v.rpartition('VERY')) print(v.rpartition('really')) # ('', '', "It's very very good") print(v.rpartition('')) # ValueError: empty separator
v = '' print(v.rpartition('er')) # ('', '', '')
<Bytes & Bytearray>:
bytes:
v = b"It's very very good" print(v.rpartition(b'er')) # (b"It's very v", b'er', b'y good') print(v.rpartition(bytearray(b'er'))) # (b"It's very v", bytearray(b'er'), b'y good') print(v.rpartition(b'very')) # (b"It's very ", b'very', b' good') print(v.rpartition(bytearray(b'very'))) # (b"It's very ", bytearray(b'very'), b' good') # ↓ print(v.rpartition(b' ')) # (b"It's very very", b' ', b'good') print(v.rpartition(bytearray(b' '))) # (b"It's very very", bytearray(b' '), b'good') # ↓↓ print(v.rpartition(b' ')) print(v.rpartition(bytearray(b' '))) print(v.rpartition(b'ER')) print(v.rpartition(bytearray(b'ER'))) print(v.rpartition(b'VERY')) print(v.rpartition(bytearray(b'VERY'))) print(v.rpartition(b'really')) print(v.rpartition(bytearray(b'really'))) # ('', '', "It's very very good") print(v.rpartition(b'')) print(v.rpartition(bytearray(b''))) # ValueError: empty separator
v = b'' print(v.rpartition(b'er')) print(v.rpartition(bytearray(b'er'))) # (b'', b'', b'')
bytearray:
v = bytearray(b"It's very very good") print(v.rpartition(b'er')) print(v.rpartition(bytearray(b'er'))) # (bytearray(b"It\'s very v"), bytearray(b'er'), bytearray(b'y good')) print(v.rpartition(b'very')) print(v.rpartition(bytearray(b'very'))) # (bytearray(b"It\'s very "), bytearray(b'very'), bytearray(b' good')) # ↓ print(v.rpartition(b' ')) print(v.rpartition(bytearray(b' '))) # (bytearray(b"It\'s very very"), bytearray(b' '), bytearray(b'good')) # ↓↓ print(v.rpartition(b' ')) print(v.rpartition(bytearray(b' '))) print(v.rpartition(b'ER')) print(v.rpartition(bytearray(b'ER'))) print(v.rpartition(b'VERY')) print(v.rpartition(bytearray(b'VERY'))) print(v.rpartition(b'really')) print(v.rpartition(bytearray(b'really'))) # (bytearray(b''), bytearray(b''), bytearray(b"It\'s very very good")) print(v.rpartition(b'')) print(v.rpartition(bytearray(b''))) # ValueError: empty separator
v = bytearray(b'') print(v.rpartition(b'er')) print(v.rpartition(bytearray(b'er'))) # (bytearray(b''), bytearray(b''), bytearray(b''))
Top comments (0)