Open In App

Python | String Split including spaces

Improve
Improve
Like Article
Like
Save
Share
Report

The problems and at the same time applications of list splitting are quite common while working with python strings. The spaces usually tend to ignore in the use cases. But sometimes, we might not need to omit the spaces but include them in our programming output. Let’s discuss certain ways in which this problem can be solved. 

Method #1: Using split() + list comprehension 

This kind of operation can be performed using the split function and list comprehension. The main difference in not omitting the space is that we specifically add the spaces that we might have omitted in the process, after each element. 

Python3




# Python3 code to demonstrate
# String Split including spaces
# using list comprehension + split()
 
# initializing string
test_string = "GfG is Best"
 
# printing original string
print("The original string : " + str(test_string))
 
# using list comprehension + split()
# String Split including spaces
res = [i for j in test_string.split() for i in (j, ' ')][:-1]
 
# print result
print("The list without omitting spaces : " + str(res))


Output

The original string : GfG is Best
The list without omitting spaces : ['GfG', ' ', 'is', ' ', 'Best']

Time complexity: O(n), where n is the length of the test_string. 
Auxiliary space complexity: O(n), where n is the length of the test_string .

Method #2 : Using zip() + chain() + cycle() 

This particular task can also be performed using the combination of above 3 functions. The zip function can be used to bind the logic, chain, and cycle function to perform the task of inserting the space at the appropriate position. 

Python3




# Python3 code to demonstrate
# String Split including spaces
# using zip() + chain() + cycle()
from itertools import chain, cycle
 
# initializing string
test_string = "GfG is Best"
 
# printing original string
print("The original string : " + str(test_string))
 
# using zip() + chain() + cycle()
# String Split including spaces
res = list(chain(*zip(test_string.split(), cycle(' '))))[:-1]
 
# print result
print("The list without omitting spaces : " + str(res))


Output : 

The original string : GfG is Best
The list without omitting spaces : ['GfG', ' ', 'is', ' ', 'Best']

Time complexity: O(n) where n is the length of the string.
Auxiliary space complexity: O(n) where n is the length of the string.

Method #3: Using replace() and split() methods

Python3




# Python3 code to demonstrate
# String Split including spaces
 
# initializing string
test_string = "GfG is Best"
 
# printing original string
print("The original string : " + str(test_string))
x=test_string.replace(" ","* *")
res=x.split("*")
     
 
# print result
print("The list without omitting spaces : " + str(res))


Output

The original string : GfG is Best
The list without omitting spaces : ['GfG', ' ', 'is', ' ', 'Best']

Time complexity: O(n), where n is the length of the input string.
Auxiliary space: O(n), where n is the length of the input string. 

Method #4: Using re

Algorithm:

  1. Import the re module
  2. Initialize the string to be split
  3. Print the original string
  4. Use re.split() to split the string including spaces. Here, we split the string on every whitespace character and preserve the whitespace as a separate element in the result list by using the capture group parentheses.
  5. Remove any empty strings from the list
  6. Print the final result

Python3




# import the re module
import re
 
# initializing string
test_string = "GfG is Best"
 
# printing original string
print("The original string : " + str(test_string))
 
# Using re.split() to split the string including spaces
res = re.split(r'(\s)', test_string)
 
# Removing empty strings from the list
res = [x for x in res if x != '']
 
# print result
print("The list without omitting spaces : " + str(res))


Output

The original string : GfG is Best
The list without omitting spaces : ['GfG', ' ', 'is', ' ', 'Best']

Time complexity: O(n), where n is the length of the input string.
Auxiliary space: O(n), where n is the length of the input string. 



Last Updated : 23 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads