12-11-2022, 12:17 PM
Python | Split String into List of Substrings
Rate this post
Summary: Use Python’s built-in split function to a given string into a list substrings. Other methods include using the library and the function.
[b]Minimal Example[/b]
Problem Formulation
Problem: Given a string containing numerous substrings. How will you split the string into a list of substrings?
Let’s understand the problem with the help of an example.
[b]Example[/b]
[b]Method 1: Using strip[/b]
Approach: Use the function where sep is the specified separator. In our case the separator is a space. Hence, you do not need to pass any separator to the function as whitespaces are considered to be default separators for the function. Therefore, whenever a space occurs the string will be split and the substring will be stored in a list.
Code:
[b]Method 2: Using re.split[/b]
The method matches all occurrences of the in the and divides the string along the matches resulting in a list of strings between the matches. For example, results in the list of strings .
Approach: Use thr method, where is the given string and ‘‘ returns a match whenever it finds a space in the string.Therefore, on every occurrence of a space the string will be split.
Code:
Related Read: Python Regex Split
[b]Method 3: Using re.findall[/b]
The method scans from left to right, searching for all non-overlapping matches of the . It returns a list of strings in the matching order when scanning the string from left to right.
Related Read: Python re.findall() – Everything You Need to Know
Approach: Use thr method, where is the given string and ‘‘ returns a match whenever it finds a normal character in the string except whitespace. Therefore, all the non-whitespace characters will be grouped together until the script encounters a space. On the occurrence of a space, the string will be split and the next group of characters that do not include a space will be searched.
Code:
Do you want to master the regex superpower? Check out my new book The Smartest Way to Learn Regular Expressions in Python with the innovative 3-step approach for active learning: (1) study a book chapter, (2) solve a code puzzle, and (3) watch an educational chapter video.
[b]Method 4: Using map[/b]
Prerequisite: The function transforms one or more iterables into a new one by applying a “transformator function” to the i-th elements of each iterable. The arguments are the transformator function object and one or more iterables. If you pass n iterables as arguments, the transformator function must be an n-ary function taking n input arguments. The return value is an iterable map object of transformed, and possibly aggregated, elements.
Related Read: Python map() — Finally Mastering the Python Map Function [+Video]
Approach: Use the function such that the iterable is the split list of substrings. This is the second argument of the map method. Now each item of this list will be passed to the method which eliminates the trailing spaces if any and then returns a map object containing the split substrings. You can convert this map object to a list using the list constructor.
Code:
Exercise
Problem: Given a string containing numerous substrings separated by commas and spaces. How will you extract the substrings and store them in a list? Note that you have to eliminate the whitespaces as well as the commas.
Hint: Python | Split String by Comma and Whitespace
Solution:
Conclusion
With that, we come to the end of this tutorial. I hope the methods discussed in this article have helped you and answered your queries. Please stay tuned and subscribe for more solutions and discussions in the future.
Happy learning!![[Image: 1f40d.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f40d.png)
But before we move on, I’m excited to present you my new Python book Python One-Liners (Amazon Link).
If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a single line of Python code. But it’s also an introduction to computer science, data science, machine learning, and algorithms. The universe in a single line of Python!
![[Image: image-1.png]](https://blog.finxter.com/wp-content/uploads/2020/02/image-1.png)
The book was released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco).
Link: https://nostarch.com/pythononeliners
Rate this post
Summary: Use Python’s built-in split function to Code:
splitCode:
regexCode:
map[b]Minimal Example[/b]
Code:
text = "Python Java Golang" # Method 1
print(text.split()) # Method 2
import re
print(re.split('\s+',text)) # Method 2.1
print(re.findall('\S+', text)) # Method 3
li = list(map(str.strip, text.split()))
res = []
for i in li: for j in i.split(): res.append(j)
print(res) # OUTPUTS: ['Python', 'Java', 'Golang']
Problem: Given a string containing numerous substrings. How will you split the string into a list of substrings?Let’s understand the problem with the help of an example.
[b]Example[/b]
Code:
# Input
text = "word1 word2 word3 word4 word5" # Output
['word1', 'word2', 'word3', 'word4', 'word5']Approach: Use the
Code:
split("sep")Code:
splitCode:
Code:
text = "word1 word2 word3 word4 word5"
print(text.split()) # ['word1', 'word2', 'word3', 'word4', 'word5']The
Code:
re.split(pattern, string)Code:
patternCode:
stringCode:
re.split('a', 'bbabbbab')Code:
['bb', 'bbb', 'b']Approach: Use thr
Code:
re.split('\s+',text)Code:
textCode:
\s+Code:
Code:
import re text = "word1 word2 word3 word4 word5"
print(re.split('\s+',text)) # ['word1', 'word2', 'word3', 'word4', 'word5']
Related Read: Python Regex Split[b]Method 3: Using re.findall[/b]
The
Code:
re.findall(pattern, string)Code:
stringCode:
pattern
Related Read: Python re.findall() – Everything You Need to KnowApproach: Use thr
Code:
re.findall('\S+',text)Code:
textCode:
\S+Code:
Code:
import re text = "word1 word2 word3 word4 word5"
print(re.findall('\S+', text)) # ['word1', 'word2', 'word3', 'word4', 'word5']Do you want to master the regex superpower? Check out my new book The Smartest Way to Learn Regular Expressions in Python with the innovative 3-step approach for active learning: (1) study a book chapter, (2) solve a code puzzle, and (3) watch an educational chapter video.
[b]Method 4: Using map[/b]
Prerequisite: The
Code:
map()
Related Read: Python map() — Finally Mastering the Python Map Function [+Video]Approach: Use the
Code:
mapCode:
stripCode:
Code:
text = "word1 word2 word3 word4 word5"
li = list(map(str.strip, text.split()))
res = []
for i in li: for j in i.split(): res.append(j)
print(res) # ['word1', 'word2', 'word3', 'word4', 'word5']Problem: Given a string containing numerous substrings separated by commas and spaces. How will you extract the substrings and store them in a list? Note that you have to eliminate the whitespaces as well as the commas.
Code:
# Input
text = "One, Two, Three"
# Output
['One', 'Two', 'Three']
Hint: Python | Split String by Comma and WhitespaceSolution:
Code:
text = "One, Two, Three"
print([x.strip() for x in text.split(',')])
# ['One', 'Two', 'Three']With that, we come to the end of this tutorial. I hope the methods discussed in this article have helped you and answered your queries. Please stay tuned and subscribe for more solutions and discussions in the future.
Happy learning!
![[Image: 1f40d.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f40d.png)
But before we move on, I’m excited to present you my new Python book Python One-Liners (Amazon Link).
If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a single line of Python code. But it’s also an introduction to computer science, data science, machine learning, and algorithms. The universe in a single line of Python!
![[Image: image-1.png]](https://blog.finxter.com/wp-content/uploads/2020/02/image-1.png)
The book was released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco).
Link: https://nostarch.com/pythononeliners

