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.strip() removes spaces from the start and end of s.
Published   July 11, 2025
🌐
W3Schools
w3schools.com › python › ref_string_strip.asp
Python String strip() Method
Python Examples Python Compiler ... Plan Python Interview Q&A Python Bootcamp Python Training ... The strip() method removes any leading, and trailing whitespaces....
🌐
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))

🌐
DigitalOcean
digitalocean.com › community › tutorials › python-remove-spaces-from-string
Effective Ways to Remove Spaces from Strings in Python | DigitalOcean
July 11, 2025 - Master Python string manipulation by learning how to remove whitespace and manage spaces with techniques like strip(), replace(), and regex for cleaner data.
🌐
Mimo
mimo.org › tutorials › python › how-to-remove-spaces-from-a-string-in-python
How to Remove Spaces from a String in Python
Avoid removing spaces when spacing carries meaning, like normal sentences you plan to display, fixed-width text formats, or anything where word boundaries matter. ... Use .strip() when the problem is extra whitespace at the start or end of a string.
Find elsewhere
🌐
DataCamp
datacamp.com › tutorial › python-trim
How to Trim a String in Python: Three Different Methods | DataCamp
February 16, 2025 - Python provides built-in methods to trim strings, making it straightforward to clean and preprocess textual data. These methods include · .strip(): Removes leading and trailing characters (whitespace by default).
🌐
Quora
quora.com › How-do-you-remove-all-whitespace-from-a-Python-string
How to remove all whitespace from a Python string - Quora
Answer (1 of 5): Using the re module: [code py] import re string_without_whitespace = re.sub(r"\s+", "", string_with_whitespace) [/code] If you're calling 'sub' very often (in a loop, let's say), you can use a compiled pattern to save a bit of cpu: [code py] pattern = re.compile(r"\s+") string_w...
🌐
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 - Q) How do I remove tabs and newline characters from a string in Python? ... For production-grade cleaning, use regex to remove all whitespace types.
🌐
Python.org
discuss.python.org › python help
Strip Command doesn't remove white space of a string stored in a variable - Python Help - Discussions on Python.org
May 1, 2022 - I tried to use the .strip() command to eliminate white spaces from a string variable and then print it. The white spaces were not removed. Can someone explain why the strip() command isn’t working where I’m going wrong a…
🌐
FavTutor
favtutor.com › blogs › remove-space-from-string-python
Remove Spaces from String in Python | FavTutor
January 6, 2022 - All whitespaces are replaced with no space("") using the replace() method. ... First, using sep as the delimiter string, we utilize the split() function to return a list of the words in the string.
🌐
Educative
educative.io › answers › how-to-strip-whitespace-in-python
How to strip whitespace in Python
For example, indentation indicates the beginning and end of a code block in Python rather than curly braces, as in other languages. To remove whitespaces in Python, we can use the strip(), rstrip(), and lstrip() methods.
🌐
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 yellow) before dot and how to get rid 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 ap…
🌐
Unix Community
community.unix.com › shell programming and scripting
Python 3.7 to remove whitespace from element - Shell Programming and Scripting - Unix Linux Community
February 7, 2023 - In the below json (format may have gotten altered) I am trying to remove the space (if present) in the id element. I added comments as well as I am learning python and wanted to get my logic down. Thank you :). file { …
🌐
CodeFatherTech
codefather.tech › home › blog › how do you remove spaces from a python string?
How Do You Remove Spaces From a Python String? - CodeFatherTech
June 27, 2025 - To replace whitespaces in a Python string with underscores you can use the following command: >>> message.replace(" ","_") '_Welcome_to_Codefather.tech_' An alternative way to remove whitespaces from a string is to use the split() and join() functions.
🌐
Sentry
sentry.io › sentry answers › python › remove whitespace from a string in python
Remove whitespace from a string in Python | Sentry
1 month ago - To remove all whitespace characters – not just regular spaces but also tabs, non-breaking spaces, hair-width spaces, etc. – we can import Python’s Regular Expressions module and use re.sub with r"\s", a regular expression that will match any whitespace character.
🌐
Vultr
docs.vultr.com › python › examples › trim-whitespace-from-a-string
Python Program to Trim Whitespace From a String | Vultr Docs
December 25, 2024 - Trimming whitespace in Python can be achieved through various methods depending on the specific needs of your project. Use Python's built-in string methods like strip(), lstrip(), and rstrip() for common whitespace issues, or turn to the re ...
🌐
Net Informations
net-informations.com › python › pro › space.htm
How to remove spaces from a string in Python
For the purpose of removing leading and trailing spaces within a string, the strip() method serves as the appropriate tool. This function effectively trims the whitespace from both the start and end of the string, resulting in a cleaner and more focused textual representation.
🌐
Wikipedia
en.wikipedia.org › wiki › Python_(programming_language)
Python (programming language) - Wikipedia
1 day ago - Python uses whitespace indentation, rather than curly brackets or keywords, to delimit blocks. An increase in indentation comes after certain statements; a decrease in indentation signifies the end of the current block. Thus, the program's visual structure accurately represents its semantic ...