Split a string on multiple delimiters in Python Last Updated : 18 Nov, 2024 Comments Improve Suggest changes Like Article Like Report In this article, we will explore various methods to split a string on multiple delimiters in Python. The simplest approach is by using re.split().Using re.split()The re.split() function from the re module is the most straightforward way to split a string on multiple delimiters. It uses a regular expression to define the delimiters. Python import re s = "apple, banana; orange grape" # Split using re.split res = re.split(r'[;,\s]+', s) print(res) Output['apple', 'banana', 'orange', 'grape'] Explanation:[;,\s]+: This pattern matches one or more occurrences of a semicolon (;), comma (,), or whitespace (\s).re.split: Splits the string wherever the pattern matches.Let's explore other methods to split a string on multiple delimiters:Table of ContentUsing translate() and split()Chaining replace() and split()Using translate() and split()If the delimiters are fixed and limited to a set of characters then we can replace them with a single delimiter (like a space) using translate() and use split(). Python s = "apple, banana; orange grape" # Replace delimiters with a space and split s = s.translate(str.maketrans({',': ' ', ';': ' '})) res = s.split() print(res) Output['apple', 'banana', 'orange', 'grape'] Explanation:str.maketrans: Creates a translation map where , and ; are replaced by a space.str.translate: Applies the translation map to the string.str.split: Splits the string on whitespaceChaining replace() and split()This method is straightforward but less efficient for handling many delimiters. Python s = "apple, banana; orange grape" # Replace each delimiter by chaining s = s.replace(',', ' ').replace(';', ' ') # split string on whitespace. res = s.split() print(res) Output['apple', 'banana', 'orange', 'grape'] Explanation:Replace each delimiter with a space using replace().Finally, split the string on whitespace. Comment More infoAdvertise with us Next Article Split a string on multiple delimiters in Python A agarwalkeshav8399 Follow Improve Article Tags : Python python-string Python string-programs Practice Tags : python Similar Reads Split and Parse a string in Python In this article, we'll look at different ways to split and parse strings in Python. Let's understand this with the help of a basic example:Pythons = "geeks,for,geeks" # Split the string by commas res = s.split(',') # Parse the list and print each element for item in res: print(item)Outputgeeks for g 2 min read Python String rsplit() Method Python String rsplit() method returns a list of strings after breaking the given string from the right side by the specified separator. It's similar to the split() method in Python, but the difference is that rsplit() starts splitting from the end of the string rather than from the beginning. Exampl 3 min read Difference Between strip and split in Python The major difference between strip and split method is that strip method removes specified characters from both ends of a string. By default it removes whitespace and returns a single modified string. Whereas, split method divides a string into parts based on a specified delimiter and by default it 1 min read Python String splitlines() method In Python, the splitlines() method is used to break a string into a list of lines based on line breaks. This is helpful when we want to split a long string containing multiple lines into separate lines. The simplest way to use splitlines() is by calling it directly on a string. It will return a list 2 min read How to split a string in C/C++, Python and Java? Splitting a string by some delimiter is a very common task. For example, we have a comma-separated list of items from a file and we want individual items in an array. Almost all programming languages, provide a function split a string by some delimiter. In C: // Splits str[] according to given delim 7 min read How to Index and Slice Strings in Python? In Python, indexing and slicing are techniques used to access specific characters or parts of a string. Indexing means referring to an element of an iterable by its position whereas slicing is a feature that enables accessing parts of the sequence.Table of ContentIndexing Strings in PythonAccessing 2 min read numpy string operations | split() function numpy.core.defchararray.split(arr, sep=None, maxsplit=None) is another function for doing string operations in numpy.It returns a list of the words in the string, using sep as the delimiter string for each element in arr. Parameters: arr : array_like of str or unicode.Input array. sep : [ str or uni 2 min read Convert string to a list in Python Our task is to Convert string to a list in Python. Whether we need to break a string into characters or words, there are multiple efficient methods to achieve this. In this article, we'll explore these conversion techniques with simple examples. The most common way to convert a string into a list is 2 min read Python String split() Python String split() method splits a string into a list of strings after breaking the given string by the specified separator.Example:Pythonstring = "one,two,three" words = string.split(',') print(words) Output:['one', 'two', 'three']Python String split() Method SyntaxSyntax: str.split(separator, m 6 min read numpy string operations | rsplit() function numpy.core.defchararray.rsplit(arr, sep=None, maxsplit=None) is another function for doing string operations in numpy. It returns a list of the words in the string, using sep as the delimiter string for each element in arr. The rsplit() method splits every string array element into a list, starting 2 min read Like