![]() |
|
[Tut] Python | Split String Multiple Whitespaces - 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 Multiple Whitespaces (/thread-100350.html) |
[Tut] Python | Split String Multiple Whitespaces - xSicKxBot - 12-06-2022 Python | Split String Multiple Whitespaces Rate this post Summary: The most efficient way to split a string using multiple whitespaces is to use the Code: splitCode: given_string.split()[b]Minimal Example:[/b] Code: import re text = "mouse\nsnake\teagle human"
# Method 1
print(text.split()) # Method 2
res = re.split("\s+", text)
print(res) # Method 3
res = re.sub(r'\s+', ',', text).split(',')
print(res) # Method 4
print(re.findall(r'\S+', text)) # ['mouse', 'snake', 'eagle', 'human'] Problem: Given a string. How will you split the string using multiple whitespaces?[b]Example[/b] Code: # Input
text = "abc\nlmn\tpqr xyz\rmno"
# Output
['abc', 'lmn', 'pqr', 'xyz', 'mno']There are numerous ways of solving the given problem. So, without further ado, let us dive into the solutions. Method 1: Using Regex The best way to deal with multiple delimiters is to use the flexibility of the regular expressions library. There are different functions available in the regex library that you can use to split the given string. Let’s go through each one by one. [b]1.1 Using re.split[/b] The Code: re.split(pattern, string)Code: patternCode: stringCode: re.split('a', 'bbabbbab')Code: ['bb', 'bbb', 'b'] Recommended Read: [b]Python Regex Split.[/b]Approach: To split the string using multiple whitespace characters use Code: re.split("\s+", text)Code: \sCode: Code: import re
text = "abc\nlmn\tpqr xyz\rmno"
res = re.split("\s+", text)
print(res) # ['abc', 'lmn', 'pqr', 'xyz', 'mno']The Code: re.findall(pattern, string)Code: stringCode: pattern Recommended Read: Python re.findall() – Everything You Need to KnowCode: Code: import re text = "abc\nlmn\tpqr xyz\rmno"
print(re.findall(r'\S+', text))Explanation: In the expression, i.e., Code: re.findall(r"\S'+", text)Code: \S+[b]1.3 Using re.sub[/b] The regex function Code: re.sub(P, R, S)Code: PCode: RCode: SCode: re.sub('a', 'b', 'aabb')Code: 'bbbb'Code: 'a'Code: 'b'Aprroach: Use the Code: re.subCode: Code: import re
text = "abc\nlmn\tpqr xyz\rmno"
res = re.sub(r'\s+', ',', text).split(',')
print(res) # ['abc', 'lmn', 'pqr', 'xyz', 'mno']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. Method 2: Using split() By default the Code: splitCode: split()Code: Code: text = "abc\nlmn\tpqr xyz\rmno"
print(text.split())
# ['abc', 'lmn', 'pqr', 'xyz', 'mno'] Recommended Digest: Python String split()Conclusion We have successfully solved the given problem using different approaches. Simply using split could do the job for you. However, feel free to explore and try out the other options mentioned above. I hope this article helped you in your Python coding journey. Please subscribe and stay tuned for more interesting articles. Happy Pythoning! Python Regex Course Google engineers are regular expression masters. The Google search engine is a massive text-processing engine that extracts value from trillions of webpages. Facebook engineers are regular expression masters. Social networks like Facebook, WhatsApp, and Instagram connect humans via text messages. Amazon engineers are regular expression masters. Ecommerce giants ship products based on textual product descriptions. Regular expressions rule the game when text processing meets computer science. If you want to become a regular expression master too, check out themost comprehensive Python regex course on the planet:
|