![]() |
|
[Tut] Python | Split String into List of Substrings - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] Python | Split String into List of Substrings (/thread-100384.html) |
[Tut] Python | Split String into List of Substrings - xSicKxBot - 12-11-2022 Python | Split String into List of Substrings 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! ![]() 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! ![]() The book was released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco). Link: https://nostarch.com/pythononeliners |