Python – String Split including spaces
Last Updated :
11 Jan, 2025
Improve
String splitting, including spaces refers to breaking a string into parts while keeping spaces as separate elements in the result.
Using regular expressions (Most Efficient)
re.split() function allows us to split a string based on a custom pattern. We can use it to split the string while capturing the spaces as separate elements.
import re
s = "Hello World Python"
# Split the string including spaces
res = re.split(r"(\s+)", s)
print(res)
6
1
import re
2
3
s = "Hello World Python"
4
# Split the string including spaces
5
res = re.split(r"(\s+)", s)
6
print(res)
Output
['Hello', ' ', 'World', ' ', 'Python']
Explanation:
- \s+ pattern matches one or more spaces.
- By wrapping \s+ in parentheses, we ensure spaces are captured as separate elements.
- This method is efficient and works well for strings with varying amounts of whitespace.
Let’s explore some more methods and see how we can split a string including spaces.
Table of Content
Using list comprehension with split()
This method uses Python’s split() function along with list comprehension to manually handle spaces.
s = "Hello World Python"
# Split the string and add spaces back as separate elements
res = [part if part.strip() else " " for part in s.split(" ")]
print(res)
4
1
s = "Hello World Python"
2
# Split the string and add spaces back as separate elements
3
res = [part if part.strip() else " " for part in s.split(" ")]
4
print(res)
Output
['Hello', 'World', 'Python']
Explanation:
- split() function splits the string by spaces.
- List comprehension ensures spaces are included in the final result.
- This method is simple but less direct than using regular expressions.
Using for loop for custom splitting
We can manually iterate through the string to split it into words and spaces by using a simple for loop.
s = "Hello World Python"
a = [] # List to store the result
temp = "" # Temporary variable to accumulate characters
for c in s:
if c != " ": # If the character is not a space
temp += c # Add character to current word
else:
a.append(temp) # Add word to result
a.append(c) # Add space as a separate element
temp = "" # Reset temp for next word
if temp: # Add the last word if exists
a.append(temp)
print(a)
16
1
s = "Hello World Python"
2
a = [] # List to store the result
3
temp = "" # Temporary variable to accumulate characters
4
5
for c in s:
6
if c != " ": # If the character is not a space
7
temp += c # Add character to current word
8
else:
9
a.append(temp) # Add word to result
10
a.append(c) # Add space as a separate element
11
temp = "" # Reset temp for next word
12
13
if temp: # Add the last word if exists
14
a.append(temp)
15
16
print(a)
Output
['Hello', ' ', 'World', ' ', 'Python']
Explanation:
- This method uses a for loop to separate words and spaces.
- Spaces are appended to the result list as separate elements.
- While longer, this method gives us precise control over how the string is split.
Using itertools.chain() for advanced splitting
We can use itertools to split a string and include spaces with some advanced handling.
from itertools import chain
s = "Hello World Python"
res = list(chain.from_iterable((word, " ") for word in s.split(" ")))
if res[-1] == " ":
res.pop() # Remove trailing space
print(res)
7
1
from itertools import chain
2
3
s = "Hello World Python"
4
res = list(chain.from_iterable((word, " ") for word in s.split(" ")))
5
if res[-1] == " ":
6
res.pop() # Remove trailing space
7
print(res)
Output
['Hello', ' ', 'World', ' ', 'Python']
Explanation:
- chain.from_iterable() function combines words and spaces into a single list.
- This method is more advanced and less intuitive but can be useful in certain cases.