Approach 1: Using split method
In Python, the split() method is typically used to divide a string into substrings. It operates by breaking the string at designated separators and returns a list of these substrings. If no separator is provided, it defaults to whitespace characters such as spaces, tabs, and newlines.
split_method.py
str = 'Hi there, How are you'
split_by_spaces = str.split()
split_by_comma = str.split(',')
print(f'split_by_spaces : {split_by_spaces}')
print(f'split_by_comma : {split_by_comma}')
Output
split_by_spaces : ['Hi', 'there,', 'How', 'are', 'you'] split_by_comma : ['Hi there', ' How are you']
Above snippet demonstrates the use of the split() method on a string to separate it into substrings based on different separators.
str = 'Hi there, How are you'
Above line defines a string variable named str containing the text "Hi there, How are you".
split_by_spaces = str.split()
Here, the split() method is called on the string str without any arguments. This splits the string into substrings based on whitespace characters (spaces, tabs, newlines, etc.). The resulting substrings are stored in a list assigned to the variable split_by_spaces.
split_by_comma = str.split(',')
Similarly, the split() method is called on the string str, but this time with a comma ',' as the separator argument. This splits the string into substrings wherever a comma is encountered. The resulting substrings are stored in a list assigned to the variable split_by_comma.
After executing these lines of code, split_by_spaces will contain ['Hi', 'there,', 'How', 'are', 'you'], and split_by_comma will contain ['Hi there', ' How are you'].
Approach 2: Using regular expressions.
Additionally, regular expressions offer an alternative method for splitting a string. This approach becomes valuable when you require splitting the string according to a more intricate pattern beyond simple whitespace.
split_by_regex.py
import re
str = 'Hi there, 123 How are 456 you'
split_by_space = re.split(r"\s+", str)
split_by_digits = re.split(r"\d+", str)
print(f'split_by_space : {split_by_space}')
print(f'split_by_digits : {split_by_digits}')
Output
split_by_space : ['Hi', 'there,', '123', 'How', 'are', '456', 'you'] split_by_digits : ['Hi there, ', ' How are ', ' you']
Above snippet demonstrate the use of regular expressions (regex) to split a string into substrings based on specific patterns:
str = 'Hi there, 123 How are 456 you'
This line initializes a string variable named str containing the text "Hi there, 123 How are 456 you".
split_by_space = re.split(r"\s+", str)
This line uses the re.split() function from the re module to split the string str using a regular expression pattern. Here, r"\s+" is the regex pattern. \s matches any whitespace character (spaces, tabs, newlines, etc.), and + indicates one or more occurrences of the preceding pattern. So, \s+ matches one or more whitespace characters. This regex pattern is used as the separator to split the string. The resulting substrings are stored in a list assigned to the variable split_by_space.
split_by_digits = re.split(r"\d+", str)
Similarly, this line uses the re.split() function with a different regex pattern r"\d+". Here, \d matches any digit character (0-9), and + again indicates one or more occurrences of the preceding pattern. So, \d+ matches one or more digits. This regex pattern is used as the separator to split the string. The resulting substrings are stored in a list assigned to the variable split_by_digits.
After executing these lines of code:
split_by_space will contain ['Hi', 'there,', '123', 'How', 'are', '456', 'you'], where the string is split based on one or more whitespace characters.
split_by_digits will contain ['Hi there, ', ' How are ', ' you'], where the string is split based on sequences of one or more digits.
No comments:
Post a Comment