Python Program to Splitting String into a Number of Sub-strings


In Python, we can split a string into substrings using the split() method. The split() method is one of the built-in Python string methods that splits a string into a list of substrings based on a specified delimiter. In this article, we will learn how to split strings into substrings with the help of Examples.

Splitting String into Substring

Method 1: Using the split() method

The split() method is a built-in method of strings in Python that splits a string into a list of sub-strings based on a specified delimiter. The delimiter can be any character or string that separates the sub-strings. The split() method takes one parameter, which is the delimiter. If no delimiter is specified, it will split the string into individual characters.

Example: split based on space and comma delimiter respectively

In the below example, we will split the string using a space delimiter. We use the split() method and pass the space delimiter to split the string based on spaces.

string = "Hello World"
substrings = string.split()
print(substrings)

string1 = "apple,banana,orange"
substrings = string1.split(",")
print(substrings)

Output

['Hello', 'World']
['apple', 'banana', 'orange']

Example: Split using regular expression

In the below example, we have used the split() function from the regular expression module to split the string “23-456-7890” into three substrings using the dash and space delimiter.

import re

string = "123-456-7890"
substrings = re.split("-|\s", string)
print(substrings)

Output

['123', '456', '7890']

Method 2: Using List comprehension

List comprehension is a concise way to create lists in Python. It allows you to create a new list based on an existing list or other iterable object, while also applying a filtering condition and performing a transformation on the elements of the iterable.

Syntax

new_list = [expression for item in iterable if condition]

Where "expression" is the transformation or operation to be performed on each element of the iterable, "item" is the current element being processed, "iterable" is the source of the elements, and "condition" is an optional filtering condition that determines whether or not an element is included in the resulting list.

Example

In the below example, we start with a string called "sentence" that contains a series of words separated by spaces. We use the split() method to split the string into a list of words, and then use list comprehension to create a new list called "words" that contains each individual word in the original string.

sentence = "The quick brown fox jumps over the lazy dog"
words = [word for word in sentence.split()]
print(words)

Output

['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']

Method 3: Using the partition() method

The partition() method is a built-in method of strings in Python that splits a string into three parts based on a specified separator. It returns a tuple that contains the left part of the string before the separator, the separator itself, and the right part of the string after the separator. If the separator is not found in the string, the method returns a tuple that contains the original string, followed by two empty strings.

Syntax

string.partition(delimiter)

Here, the string is the string that you want to split and the delimiter is the delimiter based on which you want the partition function to split the string.

Example

In the below example, we use the partition() method to split the string "Hello World!" into three parts based on the space separator. The resulting tuple contains the left part "Hello", the separator " ", and the right part "World!".

string = "Hello World!"
parts = string.partition(" ")
print(parts)

Output

('Hello', ' ', 'World!')

Method 4: Using splitlines() function

The splitlines() method is a built-in method of strings in Python that splits a string into a list of lines based on the newline character "\n". If the string does not contain any newline characters, it returns a list that contains the original string as its only element.

Syntax

string.splitlines()

Here, splitlines() does not take any parameter and always splits the string based on the new line character.

Example

In the below example, we use the splitlines() method to split the string "Line 1\nLine 2\nLine 3" into a list of lines. The resulting list contains three elements, one for each line in the original string.

string = "Line 1\nLine 2\nLine 3"
lines = string.splitlines()
print(lines)

Output

['Line 1', 'Line 2', 'Line 3']

Method 5: Using re.findall() method

The re.findall() method is a function from the re module in Python that searches a string for all occurrences of a specified regular expression pattern and returns a list of all matches. The regular expression pattern can contain various characters that represent different types of characters or sequences, allowing for complex pattern matching and extraction of specific parts of the string.

Syntax

re.findall(regular_expression, string)

Here, re.findall() method takes a regular expression to find the occurrence of matching words from the string which is also passed to the findall() function.

Example

In the below example, we use the re.findall() method to search for all occurrences of the regular expression pattern \b\w{5}\b within the string. This pattern matches any sequence of exactly five-word characters (letters, digits, or underscores) that is surrounded by word boundaries (i.e., the beginning or end of a word). The re.findall() method returns a list of all matches found in the string.

import re

string = "The quick brown fox jumps over the lazy dog"
matches = re.findall(r"\b\w{5}\b", string)
print(matches)

Output

['quick', 'brown', 'jumps']

Conclusion

In this article, we discussed how we can split a string into a number of substrings in Python using the split() function, using list comprehension, using the partition method, using the splitline method, and using re.findall() method. The split() function takes a delimiter as a parameter. The string is then split based on that delimiter. The splitline method always splits the string based on the new line delimiter. We can use any of the methods explained in this article depending on the type of splitting we want to do.

Updated on: 11-Jul-2023

83 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements