Open In App

Python | Split multiple characters from string

Improve
Improve
Like Article
Like
Save
Share
Report

In Python, Strings are a basic data type that is used to store and work with textual data. Splitting a string into numerous characters is a frequent text-processing activity in Python. While coding or improvising your programming skill, you surely must have come across many scenarios where you wished to use split() in Python not to split on only one character but multiple Delimiters at once. In this article, we will see different approaches to Python string split multiple delimiters.

Input: "GeeksForGeeks, is an-awesome! website"
Output: ['GeeksForGeeks, ', 'is', 'an-awesome!', 'website']
Explanation: In This, we are splitting the multiple delimiters from the string.

How to Split String on Multiple Delimiters in Python

In Python, We can use different approaches to split multiple Delimiters from the string. With these methods, splitting and manipulating individual characters from a string in Python is simple.

Split String By Multiple Delimiters using Split Function

In Python, we can split multiple characters from a string using split(). Here, we iterate through each delimiter and split the string using the split() function. After splitting, we join the resulting list with spaces using the join() function and we split the modified string based on whitespace to obtain the desired list of strings.

Python3




string = "GeeksForGeeks, | is an-awesome! website"
delimiters = [",", "|", ";", "!"]
 
for delimiter in delimiters:
    string = " ".join(string.split(delimiter))
 
result = string.split()
 
print(result)


Output

['GeeksForGeeks', 'is', 'an-awesome', 'website']

Python Split By Multiple Characters using replace()

In Python, we can split multiple characters from a string using replace(). This is a very rookie way of doing the split. It does not make use of regex and is inefficient but still worth a try. If you know the characters you want to split upon, just replace them with a space and then use split().

Python3




data = "Let's_try, this now"
 
# printing original string
print("The original string is : " + data)
 
# Using replace() and split()
# Splitting characters in String
res = data.replace('_', ' ').replace(', ', ' ').split()
 
print("The list after performing split functionality : " + str(res))


Output

The original string is : Let's_try, this now
The list after performing split functionality : ["Let's", 'try', 'this', 'now']

Python Split By Multiple Characters using Re.split()

In Python, we can split multiple characters from a string using resplit(). This is the most efficient and commonly used method to split multiple characters at once. It makes use of regex(regular expressions) in order to do this. 

Python3




import re
 
data = "GeeksforGeeks, is_an-awesome ! website"
 
print("The original string is : " + data)
 
# Using re.split()
# Splitting characters in String
res = re.split(', |_|-|!', data)
 
print("The list after performing split functionality : " + str(res))


Output

The original string is : GeeksforGeeks, is_an-awesome ! website
The list after performing split functionality : ['GeeksforGeeks', 'is', 'an', 'awesome ', ' website']

The line re.split(‘, |_|-|!’, data) tells Python to split the variable data on the characters: , or _ or or !. The symbol “|” represents or. There are some symbols in regex which are treated as special symbols and have different functions. If you wish to split on such a symbol, you need to escape it using a “\“(back-slash) and it needs one space before and after special characters.

 List of special characters that need to be escaped before using them:

. \ + * ? [ ^ ] $ ( ) { } = | : 

Example: In this code, we are using resplit () to split characters from strings in Python.

Python3




import re
newData1 = "GeeksforGeeks, is_an-awesome ! app + too"
 
# To split "+" with one espace before and after "+" symbol and use backslash
print(re.split(', |_|-|!|\+', newData1))
 
newData2 = "GeeksforGeeks, is_an-awesome ! app+too"
 
# To split "+" without one espace before and after "+" symbol and  use backslash
print(re.split(', |_|-|!|\+', newData2))


Output

['GeeksforGeeks', ' is', 'an', 'awesome', ' app', 'too']

Note: To know more about regex click here.

Split String By Multiple Delimiters using re.findall()

In Python, we can split multiple characters from a string using refindall(). This is a bit more arcane form but saves time. It also makes use of regex like above but instead of .split() method, it uses a method called .findall(). This method finds all the matching instances and returns each of them in a list. This way of splitting is best used when you don’t know the exact characters you want to split upon. 

Python3




import re
 
data = "This, is - another : example?!"
 
print("The original string is : " + data)
 
# Using re.findall()
# Splitting characters in String
res = re.findall(r"[\w']+", data)
 
print("The list after performing split functionality : " + str(res))


Output

The original string is : This, is - another : example?!
The list after performing split functionality : ['This', 'is', 'another', 'example']

Here the keyword [\w’]+ indicates that it will find all the instances of alphabets or underscore(_) one or more and return them in a list. Note: [\w’]+ won’t split upon an underscore(_) as it searches for alphabets as well as underscores. 

Example: In this code, we are using refindall () to split characters from strings in Python.

Python3




import re
testData = "This, is - underscored _ example?!"
print(re.findall(r"[\w']+", testData))


Output

['This', 'is', 'underscored', '_', 'example']

Character Classes

Regex cheat sheet on character description

Shorthand character class Represents
\d Any numeric digit from 0 to 9
\D Any character that is not a numeric digit from 0 to 9
\w Any letter, numeric digit, or the underscore character
\W Any character that is not a letter, numeric digit, or the underscore character
\s Any space, tab, or newline character
\S Any character that is not a space, tab, or newline


Last Updated : 01 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads