*Memos:
- My post explains a string.
split() can split a string from the left to the right as shown below:
*Memos:
- The 1st argument is
sep
(Optional-Default:None
-Type:str
orNoneType
): *Memos:- It's the delimiter of the one or more characters to delimit a string.
- An empty string cannot be set.
- The 2nd argument is
maxsplit
(Optional-Default:-1
-Type:int
): *Memos:- It decides how many splits are made.
- If it's not set or
-1
, then all possible splits are made.
- If
sep
is set, consecutive delimiters aren't grouped together and are deemed to delimit empty subsequences (for example,b'1,,2'.split(b',')
returns[b'1', b'', b'2']
).
v = 'one two three four\nfive\tsix'
# ↑ ↑↑ ↑↑↑
print(v)
# one two three four
# five six
print(v.split())
print(v.split(sep=None, maxsplit=-1))
# ['one', 'two', 'three', 'four', 'five', 'six']
print(v.split(maxsplit=0))
# ['one two three four\nfive\tsix']
print(v.split(maxsplit=1))
# ['one', 'two three four\nfive\tsix']
print(v.split(maxsplit=2))
# ['one', 'two', 'three four\nfive\tsix']
print(v.split(maxsplit=3))
# ['one', 'two', 'three', 'four\nfive\tsix']
print(v.split(maxsplit=4))
# ['one', 'two', 'three', 'four', 'five\tsix']
print(v.split(maxsplit=5))
# ['one', 'two', 'three', 'four', 'five', 'six']
# ↓
print(v.split(sep=' '))
# ['one', 'two', '', 'three', '', '', 'four\nfive\tsix']
# ↓↓
print(v.split(sep=' '))
# ['one two', 'three', ' four\nfive\tsix']
# ↓↓↓
print(v.split(sep=' '))
# ['one two three', 'four\nfive\tsix']
# ↓↓↓↓
print(v.split(sep=' '))
# ['one two three four\nfive\tsix']
print(v.split(sep=''))
# ValueError: empty separator
v = 'one-two--three---four-=*!five-=*!?six'
# ↑ ↑↑ ↑↑↑ ↑↑↑↑ ↑↑↑↑↑
print(v)
# one-two--three---four-=*!five-=*!?six
print(v.split())
# ['one-two--three---four-=*!five-=*!?six']
# ↓
print(v.split(sep='-'))
# ['one', 'two', '', 'three', '', '', 'four', '=*!five', '=*!?six']
# ↓↓
print(v.split(sep='--'))
# ['one-two', 'three', '-four-=*!five-=*!?six']
# ↓↓↓
print(v.split(sep='---'))
# ['one-two--three', 'four-=*!five-=*!?six']
# ↓↓↓
print(v.split(sep='-=*'))
# ['one-two--three---four', '!five', '!?six']
v = ''
print(v.split()) # []
print(v.split(sep=' '))
print(v.split(sep='-'))
# ['']
rsplit() can split a string from the right to the left as shown below:
*Memos:
- The 1st argument is
sep
(Optional-Default:None
-Type:str
orNoneType
): *Memos:- It's the delimiter of the one or more characters to delimit a string.
- An empty string cannot be set.
- The 2nd argument is
maxsplit
(Optional-Default:-1
-Type:int
): *Memos:- It decides how many splits are made.
- If it's not set or
-1
, then all possible splits are made.
- If
sep
is set, consecutive delimiters aren't grouped together and are deemed to delimit empty subsequences (for example,b'1,,2'.split(b',')
returns[b'1', b'', b'2']
).
v = 'one two three four\nfive\tsix'
# ↑ ↑↑ ↑↑↑
print(v)
# one two three four
# five six
print(v.rsplit())
print(v.rsplit(sep=None, maxsplit=-1))
# ['one', 'two', 'three', 'four', 'five', 'six']
print(v.rsplit(maxsplit=0))
# ['one two three four\nfive\tsix']
print(v.rsplit(maxsplit=1))
# ['one two three four\nfive', 'six']
print(v.rsplit(maxsplit=2))
# ['one two three four', 'five', 'six']
print(v.rsplit(maxsplit=3))
# ['one two three', 'four', 'five', 'six']
print(v.rsplit(maxsplit=4))
# ['one two', 'three', 'four', 'five', 'six']
print(v.rsplit(maxsplit=5))
# ['one', 'two', 'three', 'four', 'five', 'six']
# ↓
print(v.rsplit(sep=' '))
# ['one', 'two', '', 'three', '', '', 'four\nfive\tsix']
# ↓↓
print(v.rsplit(sep=' '))
# ['one two', 'three ', 'four\nfive\tsix']
# ↓↓↓
print(v.rsplit(sep=' '))
# ['one two three', 'four\nfive\tsix']
# ↓↓↓↓
print(v.rsplit(sep=' '))
# ['one two three four\nfive\tsix']
print(v.rsplit(sep=''))
# ValueError: empty separator
v = 'one-two--three---four-=*!five-=*!?six'
# ↑ ↑↑ ↑↑↑ ↑↑↑↑ ↑↑↑↑↑
print(v)
# one-two--three---four-=*!five-=*!?six
print(v.rsplit())
# ['one-two--three---four-=*!five-=*!?six']
# ↓
print(v.rsplit(sep='-'))
# ['one', 'two', '', 'three', '', '', 'four', '=*!five', '=*!?six']
# ↓↓
print(v.rsplit(sep='--'))
# ['one-two', 'three-', 'four-=*!five-=*!?six']
# ↓↓↓
print(v.rsplit(sep='---'))
# ['one-two--three', 'four-=*!five-=*!?six']
# ↓↓↓
print(v.rsplit(sep='-=*'))
# ['one-two--three---four', '!five', '!?six']
v = ''
print(v.rsplit()) # []
print(v.rsplit(sep=' '))
print(v.rsplit(sep='-'))
# ['']
Top comments (0)