![]() |
|
[Tut] Python | Split String Variable Spaces - 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 Variable Spaces (/thread-100356.html) |
[Tut] Python | Split String Variable Spaces - xSicKxBot - 12-07-2022 Python | Split String Variable Spaces Rate this post Summary: The most efficient way to split a string using variable spaces is to use the Code: splitCode: given_string.split()[b]Minimal Example[/b] Code: text = "a b c d"
# Method 1
print(text.split())
# Method 2
import re
print(re.split('\s+', text))
# Method 3
print([x for x in re.findall(r'\S+', text) if x != ''])
# Method 4
print(re.sub(r'\s+', ',', text).split(','))
# Method 5
print(list(filter(None, text.split()))) # ['a', 'b', 'c', 'd'] Problem: Given a string. How will you split the string using multiple spaces?[b]Example[/b] Code: # Input
text = "abc xyz lmn pqr"
# Output
['abc', 'xyz', 'lmn', 'pqr']The given input has multiple spaces between each substring, i.e., there are three spaces after Code: abcCode: xyzCode: lmnThough the question might look daunting at first but once you get hold of it, the solutions to this problem are easier than one can imagine. So, without further delay let us dive into the different ways of solving the given problem. Method 1: Using split() The built-in Code: split('sep')Code: splitYou can use this default property of the split function and successfully split the given string at multiple spaces just by using the Code: split()Code: Code: text = "abc xyz lmn pqr"
print(text.split()) # ['abc', 'xyz', 'lmn', 'pqr'] Recommended Digest: Python String split()Method 2: [b]Using re.split[/b] The Code: re.split(pattern, string)Code: patternCode: stringCode: re.split('a', 'bbabbbab')Code: ['bb', 'bbb', 'b']Approach: To split the string using multiple space characters use Code: re.split("\s+", text)Code: \s+Code: Code: import re
text = "abc xyz lmn pqr"
print(re.split('\s+', text))
# ['abc', 'xyz', 'lmn', 'pqr'] Recommended Read: [b]Python Regex Split.[/b][b]Method 3: Using re.findall[/b] The Code: re.findall(pattern, string)Code: stringCode: pattern Recommended Read: Python re.findall() – Everything You Need to KnowCode: Code: import re
text = "abc xyz lmn pqr"
print([x for x in re.findall(r'\S+', text) if x != ''])
# ['abc', 'xyz', 'lmn', 'pqr']The regex function Code: re.sub(P, R, S)Code: PCode: RCode: SCode: re.sub('a', 'b', 'aabb')Code: 'bbbb'Code: 'a'Code: 'b'Approach: Use the Code: re.subSilly! Isn’t it? Nevertheless, it works. Code: Code: import re
text = "abc xyz lmn pqr"
res = re.sub(r'\s+', ',', text).split(',')
print(res)
# ['abc', 'xyz', 'lmn', 'pqr']Python’s built-in Code: filter() Related Read: Python filter()Approach: You can use the Code: filter()Code: NoneCode: filterCode: filter()Code: filter()Code: list()Code: Code: text = "abc xyz lmn pqr"
print(list(filter(None, text.split())))
# ['abc', 'xyz', 'lmn', 'pqr']Hurrah! We have successfully solved the given problem using as many as five different ways. I hope you enjoyed reading this article and it helped you in your Python coding journey. Please subscribe and stay tuned for more interesting articles! Happy coding! ![]() Suggested Read: Python Regex Superpower [Full Tutorial]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. |