You are currently viewing Python String Split by Delimiter

How to split a string by a delimiter in Python? To split a string by a delimiter you can use the split() method or the re.split() function from the re (regular expressions) module. By default, the string split() method splits a string into a list of substrings at the occurrence of white space characters.

Advertisements

You can split a string by a delimiter in Python using many ways, for example, by using the split(), re.split(), splitlines(), and partition() functions. In this article, I will explain how to split a string by a delimiter by using all these methods with examples.

Related: In Python, you can split the string based on multiple delimiters.

1. Quick Examples of Splitting a String by Delimiter

If you are in a hurry, below are some quick examples of how to split a string by a delimiter.


# Quick examples of splitting a string by delimiter

# Initialize the string
string = "Welcome; to, SparkByExamples"

# Example 1: Using split() method
# Split the string by delimiter ", " 
result = string.split(", ")

# Example 2: Using split() function
# Split the string by delimiter "; "
result = string.split(";")

# Example 3: Splitting with regular expressions
result = re.split("; |, ", string)

# Example 4: Using splitlines() method
# Split the string by delimiter
string = "Welcome\nto\nSparkByExamples" 
result = string.splitlines()

# Example 5: Using partition() function
# Split string by delimiter ";"
before, delimiter, after = string.partition(";")

# Example 6: Split the string by delimiter ","
before, delimiter, after = string.partition(",")

2. Split String by Delimiter Using split() Method

Python split() method is used to split a string into a list of substrings based on a delimiter. It takes the delimiter as an argument and returns a list of substrings. By default, it splits the string at the white space character. For example, first, initialize a string variable called string with the value "Welcome; to, SparkByExamples".

Apply the split() method over the string along with the specified delimiter and get the list of substrings based on the delimiter ", ". In this case, the delimiter is ", " (comma followed by a space). It prints the result of the split operation, which is a list of substrings obtained after splitting the original string based on the specified delimiter.


# Initialize the string
string = "Welcome; to, SparkByExamples"
print("Original string:\n", string)

# Using split() method
# Split the string by specified delimiter 
result = string.split(", ")
print("After splitting the string by delimiter:\n", result)

Yields below output.

python string split delimiter

In this output, the original string is split into two substrings, "Welcome; to" and "SparkByExamples", which are separated by the delimiter ", ".

Similarly, you can split the string into substrings based on delimiter('; ') using the split(). Apply this method to get substrings for the occurrence of the delimiter ('; ' ) in the string.


# Initialize the string
string = "Welcome; to, SparkByExamples"
print("Original string:\n", string)

# Using split() function
# Split the string by delimiter ";"
result = string.split(";")
print("After splitting the string by delimiter:\n", result)

# Output:
# Original string:
#  Welcome; to, SparkByExamples
# After splitting the string by delimiter:
#  ['Welcome', ' to, SparkByExamples']

3. Splitting with Regular Expressions

Alternatively, you can use the re.split() function from the re module to split a string based on a regular expression pattern that includes both a semicolon followed by a space (; ) and a comma followed by a space (, ) as delimiters.

In the below example, first, import the re module, which provides support for regular expressions in Python. And then initialize a string variable called string with the value "Welcome; to, SparkByExamples". Apply re.split() function with the regular expression pattern "; |, " to split the string. This pattern "; |, " matches either a semicolon followed by a space or a comma followed by a space. This means the string will be split at occurrences of either of these delimiters. It prints the result of the split operation using the regular expression pattern.


import re

# Initialize the string
string = "Welcome; to, SparkByExamples"
print("Original string:\n", string)

# Splitting with regular expressions
result = re.split("; |, ", string)
print("After splitting the string by delimiter:\n", result)

Yields below output.

python string split delimiter

In this output, the original string has been split into three substrings: "Welcome", "to", and "SparkByExamples", using the specified delimiters “; ” and “, “. The regular expression pattern ensures that both delimiters are considered for splitting.

4. Split String by Delimiter Using splitlines() Method

You can also use the splitlines() method to split a string into a list of substrings using newline characters ('\n') as delimiters.

In the below example, first, initialize a string variable called string with the value "Welcome\nto\nSparkByExamples". This string contains newline characters to represent line breaks. Apply this method over the string and split it into a list of substrings for every occurrence of a new line character. The method automatically splits the string wherever it encounters newline characters, creating a list of substrings representing individual lines. It prints the result of the split operation using the splitlines() method.


# Initialize the string
string = "Welcome\nto\nSparkByExamples"
print("Original string:\n", string)

# Using splitlines() method
# Split the string by delimiter 
result = string.splitlines()
print("After splitting the string by delimiter:\n", result)

# Output:
# Original string:
# Welcome
# to
# SparkByExamples
# After splitting the string by delimiter:
# ['Welcome', 'to', 'SparkByExamples']

5. Using partition() Functions

You can also use the partition() function, to split the original string into three parts, the substring before the first occurrence of the specified delimiter, the delimiter itself, and the substring after the delimiter. This function is particularly useful when you need to split a string into parts around a specific delimiter while retaining the delimiter itself.


# Initialize the string
string = "Welcome; to, SparkByExamples"
print("Original string:\n", string)

# Using partition() function
# Split the string by delimiter ";"
before, delimiter, after = string.partition(";")
print("Before:", before)
print("Delimiter:", delimiter)
print("After:", after)

# Output:
# Original string:
#  Welcome; to, SparkByExamples
# Before: Welcome
# Delimiter: ;
# After:  to, SparkByExamples

# Split string by delimiter ","
before, delimiter, after = string.partition(",")
print("Before:", before)
print("Delimiter:", delimiter)
print("After:", after)

# Output:
# Before: Welcome; to
# Delimiter: ,
# After:  SparkByExamples

Conclusion

In this article, I have explained some of the methods of Python like split(), re.split(), splitlines(), and partition()and using these methods how to split a string by a specified delimiter with well-defined examples.

Happy Learning !!