Split a string without removing the delimiter in Python

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
4 min

banner

# Table of Contents

  1. Split a string without removing the delimiter in Python
  2. Removing the trailing delimiter with rstrip()
  3. Removing the trailing delimiter with list slicing
  4. Splitting the delimiters as separate list items
  5. Split a string without removing the delimiter using a for loop

# Split a string without removing the delimiter in Python

To split a string without removing the delimiter:

  1. Use the str.split() method to split the string into a list.
  2. Use a list comprehension to iterate over the list.
  3. On each iteration, add the delimiter to the item.
main.py
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)

split string without removing the delimiter

The code for this article is available on GitHub

The first step is to use the str.split() method to split the string into a list.

main.py
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.

main.py
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)
List comprehensions are used to perform some operation for every element, or select a subset of elements that meet a condition.

The str.split() method splits the string into a list of substrings using a delimiter.

The method takes the following 2 parameters:

NameDescription
separatorSplit the string into substrings on each occurrence of the separator
maxsplitAt most maxsplit splits are done (optional)

# Removing the trailing delimiter with rstrip()

If you need to remove the trailing delimiter, use the str.rstrip() method.

main.py
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']

removing trailing delimiter with rstrip

The code for this article is available on GitHub

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.

main.py
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.

# Removing the trailing delimiter with list slicing

You can also use list slicing to remove the trailing delimiter.

main.py
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']

removing trailing delimiter with list slicing

The code for this article is available on GitHub

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.

main.py
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_

# Splitting the delimiters as separate list items

If you need to split the delimiters as separate items in the list, use the re.split() method.

main.py
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 parentheses in the regular expression match whatever is inside and indicate the start and end of a group.

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.

# Split a string without removing the delimiter using a for loop

You can also use a for loop to split a string without removing the delimiter.

main.py
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_']
The code for this article is available on GitHub

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.

main.py
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.

main.py
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']
The code for this article is available on GitHub

I've also written an article on how to split a string with multiple delimiters.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev