Last updated: Apr 8, 2024
Reading timeยท4 min
To split a string without removing the delimiter:
str.split()
method to split the string into a list.my_str = 'bobby_hadz_com' delimiter = '_' my_list = [x+delimiter for x in my_str.split(delimiter) if x] # ๐๏ธ ['bobby_', 'hadz_', 'com_'] print(my_list)
The first step is to use the str.split()
method to split the string into a
list.
my_str = 'bobby_hadz_com' delimiter = '_' # ๐๏ธ ['bobby', 'hadz', 'com'] print(my_str.split(delimiter))
Once we have a list of strings, we can use a list comprehension to iterate over it, adding the delimiter to each list item.
my_str = 'bobby_hadz_com' delimiter = '_' my_list = [x+delimiter for x in my_str.split(delimiter) if x] # ๐๏ธ ['bobby_', 'hadz_', 'com_'] print(my_list)
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) |
If you need to remove the trailing delimiter, use the str.rstrip()
method.
my_str = 'bobby_hadz_com' delimiter = '_' my_list = [x+delimiter for x in my_str.split(delimiter) if x] # ๐๏ธ ['bobby_', 'hadz_', 'com_'] print(my_list) my_list[-1] = my_list[-1].rstrip(delimiter) print(my_list) # ๐๏ธ ['bobby_', 'hadz_', 'com']
We used the str.rstrip()
method to remove the trailing delimiter from the last
list element.
The str.rstrip() method takes a string containing characters as an argument and returns a copy of the string with the specified trailing characters removed.
my_str = 'bobbyhadz.com' result = my_str.rstrip('ocm.') print(result) # ๐๏ธ 'bobbyhadz'
The method doesn't change the original string, it returns a new string. Strings are immutable in Python.
You can also use list slicing to remove the trailing delimiter.
my_str = 'bobby_hadz_com' delimiter = '_' my_list = [x+delimiter for x in my_str.split(delimiter) if x] # ๐๏ธ ['bobby_', 'hadz_', 'com_'] print(my_list) my_list[-1] = my_list[-1][:-len(delimiter)] print(my_list) # ๐๏ธ ['bobby_', 'hadz_', 'com']
The syntax for list slicing is a_list[start:stop:step]
.
We only specified a negative stop
index in the slice.
Negative stop indices can be used to omit the last N characters.
my_str = 'bobby_hadz_com' print(my_str[:-1]) # bobby_hadz_co print(my_str[:-2]) # bobby_hadz_c print(my_str[:-3]) # bobby_hadz_
If you need to split the delimiters as separate items in the list, use the
re.split()
method.
import re my_str = 'bobby_hadz_com' my_list = re.split(r'(_)', my_str) # ๐๏ธ ['bobby', '_', 'hadz', '_', 'com'] print(my_list)
The re.split() method takes a pattern and a string and splits the string on each occurrence of the pattern.
The group's contents can still be retrieved after the match.
Even though we split the string on the underscore, we still include the underscores in the result.
You can also use a for loop to split a string without removing the delimiter.
my_str = 'bobby_hadz_com' my_list = [] delimiter = '_' for item in my_str.split(delimiter): if item: my_list.append(item + delimiter) print(my_list) # ๐๏ธ ['bobby_', 'hadz_', 'com_']
We used a for
loop to iterate over the result of calling split()
on the
string.
On each iteration, we append the current item and the delimiter to a new list.
You can use the str.rstrip()
method if you need to remove the delimiter from
the last list item.
my_str = 'bobby_hadz_com' my_list = [] delimiter = '_' for item in my_str.split(delimiter): if item: my_list.append(item + delimiter) print(my_list) # ๐๏ธ ['bobby_', 'hadz_', 'com_'] my_list[-1] = my_list[-1].rstrip(delimiter) print(my_list) # ๐๏ธ ['bobby_', 'hadz_', 'com']
The last element in a list has an index of -1
, so we reassigned it to the
result of calling rstrip()
on it.
You can also use list slicing to remove the trailing delimiter.
my_str = 'bobby_hadz_com' my_list = [] delimiter = '_' for item in my_str.split(delimiter): if item: my_list.append(item + delimiter) print(my_list) # ๐๏ธ ['bobby_', 'hadz_', 'com_'] my_list[-1] = my_list[-1][:-len(delimiter)] print(my_list) # ๐๏ธ ['bobby_', 'hadz_', 'com']
I've also written an article on how to split a string with multiple delimiters.
You can learn more about the related topics by checking out the following tutorials: