If you want to remove leading and ending whitespace, use str.strip():

>>> "  hello  apple  ".strip()
'hello  apple'

If you want to remove all space characters, use str.replace() (NB this only removes the “normal” ASCII space character ' ' U+0020 but not any other whitespace):

>>> "  hello  apple  ".replace(" ", "")
'helloapple'

If you want to remove all whitespace and then leave a single space character between words, use str.split() followed by str.join():

>>> " ".join("  hello  apple  ".split())
'hello apple'

If you want to remove all whitespace then change the above leading " " to "":

>>> "".join("  hello  apple  ".split())
'helloapple'
Answer from Cédric Julien on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-remove-spaces-from-a-string
Remove spaces from a string in Python - GeeksforGeeks
Explanation: s.replace(" ", "") replaces every space in s with an empty string "", effectively removing them.
Published   July 11, 2025
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-remove-spaces-from-string
Effective Ways to Remove Spaces from Strings in Python | DigitalOcean
July 11, 2025 - Remove all spaces using regex: HelloWorldFromDigitalOceanHiThere Remove leading spaces using regex: Hello World From DigitalOcean Hi There Remove trailing spaces using regex: Hello World From DigitalOcean Hi There Remove leading and trailing spaces using regex: Hello World From DigitalOcean Hi There · When removing whitespace from strings, especially in performance-critical applications or when processing large volumes of text, the efficiency of the method you choose matters. This section compares the performance of the primary methods discussed and provides guidance on when to use each one. We will use Python’s built-in timeit module to benchmark the replace(), join()/split(), translate(), and re.sub() methods.
🌐
Accuweb
accuweb.cloud › home › how to remove spaces from a string in python (complete 2026 guide)
How to Remove Spaces From a String in Python (Complete 2026 Guide)
December 1, 2023 - That single line removes all whitespace characters from a Python string and is the most robust answer for most real-world use cases. ... Understanding the difference is critical for choosing the correct method. The replace() method replaces occurrences of a substring with another substring. To remove literal spaces: s = "W hi te space" newStr = s.replace(" ", "") print(newStr) Output: WhitespaceCopy
🌐
Reddit
reddit.com › r/learnpython › methods to remove spaces from text
r/learnpython on Reddit: methods to remove spaces from text
December 20, 2023 -

Hi everyone, I recently took the Fullstack Python Developer course and am now taking my first steps on this path.
Can you tell me what other methods of removing spaces from text you know and in which case, each of them is better to use?
So far I know 4 of them:

  1. replace method -> text_wo_spaces = text.replace(" ","")

  2. join and split methods -> text_wo_spaces = ' '.join(text.split())

  3. list generator + join -> text_wo_spaces = ' '.join([char for char in text if char != ' '])

  4. filter + join methods -> text_wo_spaces = ' '.join(filter(lambda char : != ' ', text))

🌐
Python Morsels
pythonmorsels.com › remove-spaces
How to remove spaces in Python - Python Morsels
January 25, 2022 - The string join method can join an iterable of strings by a delimiter (see convert a list to a string in Python). If we join with a delimiter of empty string (""), we'll effectively remove all spaces:
🌐
Mimo
mimo.org › tutorials › python › how-to-remove-spaces-from-a-string-in-python
How to Remove Spaces from a String in Python
If you want to delete regular space characters (" ") everywhere in the string, use .replace(" ", ""). ... This also removes normal spaces and can feel easier to read.
Find elsewhere
🌐
FavTutor
favtutor.com › blogs › remove-space-from-string-python
Remove Spaces from String in Python | FavTutor
January 6, 2022 - Using this method, only the trailing spaces can be removed in a string. ... The string strip() method can be used to eliminate spaces from both the beginning and end of a string. Learn more about rstrip in python here.
🌐
Net Informations
net-informations.com › python › pro › space.htm
How to remove spaces from a string in Python
To effectively eliminate all types of whitespace, encompassing spaces, tabs, and carriage return-line feed (CRLF) characters, an elegant and concise solution involves utilizing the translate() method. This function allows for a one-liner approach to achieving comprehensive whitespace removal, ...
🌐
AskPython
askpython.com › python › string › python-remove-spaces-from-string
Python Remove Spaces from String - AskPython
February 16, 2023 - Either of the following techniques can be used to get rid of the spaces from a string: ... The split() function basically removes the leading and the trailing spaces of the string.
🌐
Python.org
discuss.python.org › python help
How to remove extra space - Python Help - Discussions on Python.org
September 19, 2022 - Why there is extra spaces (marked ... of them? x = float(input(“Give a decimal number: “)) y = int(input(“Give an integer number: “)) result = x**y print(x,“to the power of”,y,“is approximately”,(round(result)),”.”) print(“To be exact it is”,(round(result, 2)),”.”)...
🌐
Scaler
scaler.com › home › topics › how to remove whitespace from string in python?
How To Remove Spaces from a String in Python? - Scaler Topics
February 14, 2024 - Here's how you can use the isspace() method to trim extra spaces from a string: Initialize an empty string to store the result. Iterate over each character in the original string using a for loop. Check each character with the isspace() method to determine if it is a whitespace character.
🌐
Medium
medium.com › techtofreedom › 5-ways-to-remove-spaces-of-a-python-string-633978255df1
5 Ways To Remove Spaces of a Python String | by Yang Zhou | TechToFreedom | Medium
July 27, 2021 - More importantly, you will feel the flexibility and elegance of Python again. The simplest case is that all the leading and trailing spaces of a string are unnecessary. For this, we can just use the strip() function to remove them all.
🌐
CloudSigma
blog.cloudsigma.com › removing-spaces-in-python
Removing Spaces in Python — CloudSigma
May 16, 2023 - If you want to remove just the leading or tailing spaces, then use lstrip() or rstrip() respectively:
🌐
Altcademy
altcademy.com › blog › how-to-remove-spaces-from-a-string-in-python
How to remove spaces from a string in Python - Altcademy.com
June 13, 2023 - This method is quite efficient, especially for long strings with multiple spaces between words. However, it may not be suitable for strings with special characters, as it could change the original order of characters. Regular expressions, often abbreviated as regex, are a powerful tool for text processing. They allow us to search, match, and manipulate text based on specific patterns. In this method, we will use the re module in Python to remove spaces from a string.
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python remove spaces from string
Python Remove Spaces From String - Spark By {Examples}
May 21, 2024 - How to remove spaces from a string in Python? Removing spaces from a string involves eliminating any whitespace characters present within the string.
🌐
Hostman
hostman.com › tutorials › how-to-remove-spaces-from-a-string-in-python
How to Remove spaces from a string in Python | Guide by Hostman
In this tutorial, we will explore various methods for removing all white spaces from a Python string.
🌐
Syntx Scenarios
syntaxscenarios.com › home › python › how to remove spaces from a string in python?
How To Remove Spaces From a String in Python?
January 4, 2025 - In Python, one of the easiest ways to remove spaces from a string is by using the strip() method, which is a pre-defined function. The strip() method removes all leading spaces (spaces at the beginning of a string) and trailing spaces (spaces ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-remove-unwanted-spaces-from-string
Python | Remove unwanted spaces from string - GeeksforGeeks
April 23, 2023 - Method #1: Using re.sub() This problem can be performed using the regex in which we can restrict the separation between the words to be just a single space using the appropriate regex string.
🌐
W3Schools
w3schools.in › python-tutorial › remove-space-from-a-string-in-python
Remove Space From a String in Python
These three methods do not remove empty spaces between the strings and are usually used where the input is taken from the user. ... name = ' Chris Gayle ' #remove spaces from left print (name.lstrip()) #remove spaces from right print (name.rstrip()) #remove spaces from both side print (name.strip())