Last updated: Apr 8, 2024
Reading timeยท4 min
To split a string and get the first element:
str.split()
method, setting maxsplit
to 1.0
.my_str = 'bobby_hadz_com' first = my_str.split('_', 1)[0] print(first) # ๐๏ธ 'bobby'
The str.split() method splits the string into a list of substrings using a delimiter.
The method takes the following 2 parameters:
Name | Description |
---|---|
separator | Split the string into substrings on each occurrence of the separator |
maxsplit | At most maxsplit splits are done (optional) |
When the maxsplit
argument is set to 1
, at most 1 split is done.
my_str = 'a_b_c_d' # ๐๏ธ ['a', 'b_c_d'] print(my_str.split('_', 1))
Python indexes are zero-based, so the first character in a string has an index
of 0
, and the last character has an index of -1
or len(a_string) - 1
.
If the separator is not found in the string, a list containing only 1 element is returned.
my_str = 'abcd' first = my_str.split('_', 1)[0] print(first) # ๐๏ธ 'abcd'
If your string starts with the specific separator, you might get a confusing result.
my_str = '_a_b_c_d_' # ๐๏ธ ['', 'a_b_c_d_'] print(my_str.split('_', 1)) first = my_str.split('_', 1)[0] print(repr(first)) # ๐๏ธ ""
You can use the str.strip()
method to remove the leading or trailing
separator.
my_str = '_a_b_c_d_' # ๐๏ธ ['a', 'b_c_d'] print(my_str.strip('_').split('_', 1)) first = my_str.strip('_').split('_', 1)[0] print(first) # ๐๏ธ "a"
We used the str.strip()
method to remove any leading or trailing underscores
from the string before calling the split()
method.
To split a string and get the last element:
str.rsplit()
method to split the string from the right.maxsplit
argument to 1.-1
.my_str = 'bobby,hadz,com' last = my_str.rsplit(',', 1)[-1] print(last) # ๐๏ธ 'com'
We used the rsplit()
method to split the string from the right.
The str.rsplit() method returns a list of the words in the string using the provided separator as the delimiter string.
my_str = 'bobby hadz com' print(my_str.rsplit(' ')) # ๐๏ธ ['bobby', 'hadz', 'com'] print(my_str.rsplit(' ', 1)) # ๐๏ธ ['bobby hadz', 'com']
The method takes the following 2 arguments:
Name | Description |
---|---|
separator | Split the string into substrings on each occurrence of the separator |
maxsplit | At most maxsplit splits are done, the rightmost ones (optional) |
Except for splitting from the right, rsplit()
behaves like split()
.
When the maxsplit
argument is set to 1
, at most 1 split is done.
The last step is to access the last element in the list by accessing the list
item at index -1
.
my_str = 'bobby,hadz,com,abc' last = my_str.rsplit(',', 1)[-1] print(last) # ๐๏ธ 'abc'
Python indexes are zero-based, so the first character in a string has an index
of 0
, and the last character has an index of -1
or len(a_string) - 1
.
You can also use the str.split()
method in a similar way.
my_str = 'bobby-hadz-com' last = my_str.split('-')[-1] print(last) # ๐๏ธ 'com'
If your string ends with the specific separator, you might get a confusing result.
my_str = 'bobby-hadz-com-' last = my_str.rsplit('-', 1)[-1] # ๐๏ธ ['bobby-hadz-com', ''] print(my_str.rsplit('-', 1)) print(last) # ๐๏ธ ""
You can use the str.strip()
method to remove the leading or trailing
separator.
my_str = 'bobby-hadz-com-' last = my_str.strip('-').rsplit('-', 1)[-1] print(last) # ๐๏ธ "com"
We used the str.strip()
method to remove any leading or trailing hyphens from
the string before calling the rsplit()
method.
You can also use the str.partition()
method to split a string and get the
first element.
my_str = 'bobby-hadz-com' result = my_str.partition('-')[0] print(result) # ๐๏ธ bobby
The str.partition() method splits the string at the first occurrence of the provided separator.
my_str = 'bobby!hadz!com' separator = '!' # ๐๏ธ ('bobby', '!', 'hadz!com') print(my_str.partition(separator))
The method returns a tuple containing 3 elements - the part before the separator, the separator, and the part after the separator.
my_str = 'bobby-hadz-com' # ๐๏ธ ('bobby-hadz-com', '', '') print(my_str.partition('!'))
In our case, the expression would return the entire string if the separator is not contained in the string.
my_str = 'bobby-hadz-com' result = my_str.partition('!')[0] print(result) # ๐๏ธ bobby-hadz-com
You can also use the str.rpartition
method to split a string and get the last
element.
my_str = 'bobby-hadz-com' result = my_str.rpartition('-')[-1] print(result) # ๐๏ธ com
The str.rpartition() method splits the string at the last occurrence of the provided separator.
my_str = 'bobbyhadz.com/articles/python' result = my_str.rpartition('/')[2] print(result) # ๐๏ธ 'python' # ๐๏ธ ('bobbyhadz.com/articles', '/', 'python') print(my_str.rpartition('/'))
The method returns a tuple containing 3 elements - the part before the separator, the separator, and the part after the separator.
my_str = 'bobby-hadz-com' # ๐๏ธ ('', '', 'bobby-hadz-com') print(my_str.rpartition('!'))
Accessing the tuple at an index of -1
would return the entire string if the
separator is not contained in the string.
my_str = 'bobby-hadz-com' result = my_str.rpartition('1')[-1] print(result) # ๐๏ธ bobby-hadz-com
I've also written an article on how to split a string and remove the whitespace.
You can learn more about the related topics by checking out the following tutorials: