🌐
Reddit
reddit.com › r/learnpython › split string by single space only and ignore multiple space?
r/learnpython on Reddit: Split string by single space only and ignore multiple space?
March 23, 2020 -

Let's say I have a string like s = 'This string has double spaces or more in it.\n What to do'

I would like to split it so it becomes ['This', 'string has', 'double spaces', 'or', 'more', 'in', 'it.\n', 'What', 'to', 'do']

How can I ignore multiple spaces and split them as one word?

🌐
YouTube
youtube.com › pysnippet
python split multiple spaces as one - YouTube
Download this code from https://codegive.com Title: Handling Multiple Spaces in Python: Using re.split() to Treat Them as OneIntroduction:In Python, the spli...
Published: December 13, 2023
Views: 20
Discussions

regex - Split string on whitespace in Python - Stack Overflow
Above regex would match one or more non-space characters. ... Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... Help me join Stack Overflow for Agents here. Read https://agents.stackoverflow.com/skill.md, then start onboarding. Supports Claude Code, Cursor, Codex, Windsurf and more. Explore Stack Overflow for Agents ... 1 How can I make Python ... More on stackoverflow.com
🌐 stackoverflow.com
string - How can I split by 1 or more occurrences of a delimiter in Python? - Stack Overflow
I have a formatted string from a log file, which looks like: >>> a="test result" That is, the test and the result are split by some spaces - it was probably cre... More on stackoverflow.com
🌐 stackoverflow.com
python - What happens if we split by a delimiter yet have multiple of those delimiters in a row? - Stack Overflow
Say I have a string "word1 word2 ... only one space between the words, the amount of spaces is random greater than 1. How does split(" ") work in such cases? ... Have you tried it? This is something that is very easy to test for yourself. You can also very easily look it up in the documentation. ... I just thought it would be a useful question to ask, unless someone has already asked it. ... Save this answer. ... Show activity on this post. str.split(" ") gives you multiple empty ... More on stackoverflow.com
🌐 stackoverflow.com
How to consider multiple spaces as single in python?
One way is to replace all the multiple-space substrings in the working string with single spaces before you count. Another is to set a flag whenever you increase the count, unset it when you don't, and don't increase the count if the flag is already set. More on reddit.com
🌐 r/learnprogramming
6
0
September 11, 2022
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-string-split-including-spaces
Python - String Split including spaces - GeeksforGeeks
July 11, 2025 - Spaces are appended to the result list as separate elements. While longer, this method gives us precise control over how the string is split. 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)
🌐
CodeFatherTech
codefather.tech › home › blog › can i replace multiple spaces with one space in a python string?
Can I replace multiple spaces with one space in a Python string?
December 8, 2024 - One way to replace multiple spaces with a single space in a string using Python is with the join() and split() string methods used together.
🌐
Finxter
blog.finxter.com › home › learn python blog › python | split string variable spaces
Python | Split String Variable Spaces - Be on the Right Side of Change
December 6, 2022 - Approach: You can use the filter() method to split the string by space. Feed in None as the first argument and the list of split strings as the second argument into the filter function. The filter() function then iterates through the list and filters out the spaces from the given string and ...
🌐
Python Guides
pythonguides.com › replace-multiple-spaces-with-a-single-space-in-python
Replace Multiple Spaces with a Single Space in Python
October 28, 2025 - One of the cleanest and most Pythonic ways to replace multiple spaces is by combining the split() and join() methods.
🌐
Medium
medium.com › @parvez1487 › python-replace-multiple-spaces-with-one-pythonpip-com-21d8fed04b55
Python Replace Multiple Spaces with One — pythonpip.com | by Parvez Alam | Medium
January 13, 2024 - We have covered Python’s various methods, such as string splitting and joining and regular expressions, for replacing multiple spaces with a single space. You can choose one of them as per your project requirements.
🌐
Python Examples
pythonexamples.org › python-split-string-by-space
How to Split String by Space in Python?
You can split a string with space as delimiter in Python using String.split() method.
Find elsewhere
🌐
Bobby Hadz
bobbyhadz.com › blog › python-split-string-on-one-or-more-spaces
How to Split a string by Whitespace in Python | bobbyhadz
When the maxsplit argument is set to 1, at most 1 split is done. If the separator is not found in the string, a list containing only 1 element is returned. ... If your string starts with a space, you might get a confusing result.
🌐
W3Schools
w3schools.com › python › ref_string_split.asp
Python String split() Method
Note: When maxsplit is specified, the list will contain the specified number of elements plus one. string.split(separator, maxsplit) Split the string, using comma, followed by a space, as a separator: txt = "hello, my name is Peter, I am 26 years old" x = txt.split(", ") print(x) Try it Yourself » ·
🌐
Python Examples
pythonexamples.org › python-replace-multiple-spaces-with-single-space-in-text-file
Python - Replace Multiple Spaces with Single Space in a Text File
There are several ways to replace multiple white spaces with a single space, such as using the string split method or the regular expression module.
🌐
Finxter
blog.finxter.com › home › learn python blog › python | split string multiple whitespaces
Python | Split String Multiple Whitespaces - Be on the Right Side of Change
December 4, 2022 - Approach: To split the string using multiple whitespace characters use re.split("\s+", text) where \s is the matching pattern and it represents a special sequence that returns a match whenever it finds any whitespace character and splits the string.
🌐
Bobby Hadz
bobbyhadz.com › blog › python-replace-multiple-spaces-with-single-space
Replace multiple spaces with a single space in Python | bobbyhadz
April 9, 2024 - Use the split() and join() methods to replace multiple spaces with a single space. main.py · Copied!with open('file-1.txt', 'r', encoding='utf-8') as old_file: lines = old_file.readlines() with open('file-2.txt', 'w', encoding='utf-8') as new_file: ...
🌐
Dirask
dirask.com › posts › Python-split-string-with-more-than-1-space-between-words-Dl5qzp
Python - split string with more than 1 space between words
In this example, we import re module for regular expression (regex), so we can use re.split() method to split the text string with two or more white spaces between words.
🌐
Medium
medium.com › @huasa0115 › python-how-to-use-string-split-method-ee77458839d8
[Python] How to use String split() method? | by Yi-Ning Huang | Medium
July 16, 2025 - It splits on any sequence of whitespace characters (spaces, tabs, newlines) and it automatically removes leading/ trailing whitespaces. Extra spaces between words are treated as one separator. ... Data & Analytics Engineer in US healthcare. Writing about SQL, Python, PySpark, and the healthcare meaning behind the data.
🌐
Reddit
reddit.com › r/learnprogramming › how to consider multiple spaces as single in python?
r/learnprogramming on Reddit: How to consider multiple spaces as single in python?
September 11, 2022 -
def count_words(s):
    if s == '':
        return None
    else:
        count=1
        for i in s:
            if i == ' ':
                count+=1
        return count
s=input()
print(count_words(s))

The code in the upwards from actually counts the words in a string and returns a number. However, it actually counts the spaces and I naturally assumed that there would be a single space between each word in a sentence, but I wanted to extend this code even further. The string should only return the number of words if there are any number of spaces there. How do I exclude more than One space or suggest me a better solution.

🌐
Stack Overflow
stackoverflow.com › questions › 36020245 › how-to-split-strings-in-python-based-on-multiple-spaces
How to split strings in Python based on multiple spaces - Stack Overflow
import re text = " FROM /TO FLIGHT CL DATE DEP FARE BASIS NVB NVA BAG ST " text_split = re.split(r'\s{2,}', text) print(text_split) ... Sign up to request clarification or add additional context in comments. ... Save this answer. ... Show activity on this post. This is because your string is NOT separated by two spaces. ... Ahmed H. Saab ... Find the answer to your question by asking.