Python | String Split including spaces
The problems and at the same time applications of list splitting is quite common while working with python strings. The spaces are 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 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)) |
The original string : GfG is Best The list without omitting spaces : ['GfG', ' ', 'is', ' ', 'Best']
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 perform the task of inserting the space at appropriate position.
# 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)) |
The original string : GfG is Best The list without omitting spaces : ['GfG', ' ', 'is', ' ', 'Best']
Recommended Posts:
- Python | Check for spaces in string
- Python | Remove spaces from a string
- Python | Remove unwanted spaces from string
- Python code to move spaces to front of string in single traversal
- Python String | split()
- How to split a string in C/C++, Python and Java?
- Python | Split string into list of characters
- Python program to split and join a string
- Python | Split given string into equal halves
- Python | Split multiple characters from string
- Python | Split string on Kth Occurrence of Character
- Python | Split string in groups of n consecutive characters
- Python | Split CamelCase string to individual strings
- Split a string in equal parts (grouper in Python)
- Python | Pandas Split strings into two List/Columns using str.split()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



