How To Split A String Without Removing The Delimiter In Python

Split a string without removing the delimiter in Python

Today, I would like to show you how to split a string without removing the delimiter in Python by using the re.split(), list comprehension, the splitlines() and re.findall() functions. Please read the following article.

Split a string without removing the delimiter in Python

Use the re.split function

Example:

  • Import the re module.
  • Initialize a string of delimiters.
  • Use the re.split() function with characters: () to store delimiters and letters. \W to find the delimiters in the string when splitting.
import re

myString = 'visit$learnshareit##website'

# Use the re.split() function
print('String after splitting and keeping the delimiters:', re.split('(\W)', myString))

Output:

String after splitting and keeping the delimiters: ['visit', '$', 'learnshareit', '#', '', '#', 'website']

Or you can use another regular expression like the following, which has the same result.

Example:

import re

myString = 'visit$learnshareit##website'

# Use the re.split() function
print('String after splitting and keeping the delimiters:', re.split('([^a-zA-Z0-9])', myString))

Output:

String after splitting and keeping the delimiters: ['visit', '$', 'learnshareit', '#', '', '#', 'website']

In the above example ([^a-zA-Z0-9]), it means: () stores the separator and letters. [ ] compares the set with the characters in the string. ^a-zA-Z0-9 matches any character that is not a letter or number.

Use list comprehension

Example:

  • Initialize a string of delimiters.
  • Use list comprehension to split the string and keep the delimiter.
myString = 'visit#learnshareit#website'

# Use list comprehension to split the string and keep the delimiter
print('String after splitting and keeping the delimiters:') 

print([u for x in myString.split('#') for u in (x, '#')])

Output:

String after splitting and keeping the delimiters:
['visit', '#', 'learnshareit', '#', 'website', '#']

Use the splitlines() function

Syntax:

str.splitlines(num=string.count('\n'))

Example:

  • Initializes a list of both strings and None.
  • The splitlines() function will return a string with the newline character ‘\n’ if the delimiter needs to be displayed as a line break.
myString = """
Visit.
learnshareit.
website."""

# Use the splitlines() function to preserve newline characters '\n\
print(myString.splitlines(True))

Output:

['\n', 'Visit.\n', 'learnshareit.\n', 'website.']

Use the re.findall function

Syntax:

re.findall(regex, string))

Parameters:

  • regex: regular expression to search for digits.
  • string: string you want the regular expression to search for.

The findall() function returns a list containing the pattern matches in the string. If not found, the function returns an empty list.

Example:

  • Import the re module.
  • Initialize a string of delimiters.
  • Use the findall() function to split the string and keep the delimiter.
import re
 
myString = 'visit#learnshareit#website'
 
# Use the re.findall() function to split the string still retaining the delimiter
print('String after splitting and keeping the delimiters:', re.findall('[\D+\W]', myString))

Output:

String after splitting and keeping the delimiters: ['v', 'i', 's', 'i', 't', '#', 'l', 'e', 'a', 'r', 'n', 's', 'h', 'a', 'r', 'e', 'i', 't', '#', 'w', 'e', 'b', 's', 'i', 't', 'e']

Summary

The tutorial split a string without removing the delimiter in Python has ended. I think the best way for you is to use re.split() function.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *