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
If we only want to remove spaces from the beginning of the string, we can use lstrip().
Published: July 11, 2025
Discussions

Strip Command doesn't remove white space of a string stored in a variable
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 and what is the right way to use this command? More on discuss.python.org
🌐 discuss.python.org
6
0
May 1, 2022
methods to remove spaces from text
You are missing a regex. And the best one is number 1. Because of readability. Plus numbers 2, 3 and 4 don't work. You are putting the space back. More on reddit.com
🌐 r/learnpython
16
6
December 20, 2023
How to Remove Spaces From a String in Python (Complete 2026 Guide)
This does not affect spaces inside or at the end of the string. ... Use rstrip() to remove whitespace from the right side. ... This removes whitespace only from the beginning and end. Q) How do I remove multiple consecutive spaces in Python? More on accuweb.cloud
🌐 accuweb.cloud
1
December 1, 2023
Pls how do i remove white space between two words in python
Learn more efficiently, for free: · Introduction to Python More on sololearn.com
🌐 sololearn.com
13
5
November 1, 2019
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-remove-spaces-from-string
Effective Ways to Remove Spaces from Strings in Python | DigitalOcean
Master Python string manipulation by learning how to remove whitespace and manage spaces with techniques like strip(), replace(), and regex for cleaner data.
🌐
W3Schools
w3schools.com › python › ref_string_strip.asp
Python String strip() Method
Remove List Duplicates Reverse ... Python Study Plan Python Interview Q&A Python Training ... The strip() method removes any leading, and trailing whitespaces......
🌐
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…
🌐
DEV Community
dev.to › trinityyi › how-do-i-trim-whitespace-from-a-string-in-python-309o
How do I trim whitespace from a string in Python? - DEV Community
September 15, 2022 - Leading whitespace characters are the whitespace characters at the start of a string. To remove them, use the str.lstrip() method.
Find elsewhere
🌐
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))

🌐
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. ... What to look for: split(" ") only targets normal spaces. Tabs and newlines remain. If input can contain tabs, newlines, or mixed whitespace, use split() with no argument.
🌐
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...
🌐
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.
🌐
Sentry
sentry.io › sentry answers › python › remove whitespace from a string in python
Remove whitespace from a string in Python | Sentry
July 3, 2026 - To remove all whitespace characters ... Python’s Regular Expressions module and use re.sub with r"\s", a regular expression that will match any whitespace character....
🌐
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 - This does not affect spaces inside or at the end of the string. ... Use rstrip() to remove whitespace from the right side. ... This removes whitespace only from the beginning and end. Q) How do I remove multiple consecutive spaces in Python?
🌐
JanBask Training
janbasktraining.com › community › python-python › remove-all-whitespace-in-a-string-in-python
Remove all whitespace in a string in Python | JanBask Training Community
July 8, 2021 - I want to eliminate all the whitespace from a string, on both ends, and in between words.I have this Python code:def my_handle(self): sentence = ' hello apple ' sentence.
🌐
Java2Blog
java2blog.com › home › python › remove whitespace from string python
Remove whitespace from String Python - Java2Blog
February 24, 2021 - You can use String’s replace method to remove all whitespaces from String in Python.
🌐
Altcademy
altcademy.com › blog › how-to-remove-spaces-from-a-string-in-python
How to remove spaces from a string in Python
June 13, 2023 - In this tutorial, we have explored different techniques to remove spaces from a string in Python. Each method has its own advantages and disadvantages, so it's essential to choose the one that best fits your specific requirements. Here's a quick summary of the methods we have covered: str.replace(): Simple and easy to use; only removes spaces in the middle of the string.
🌐
Codingem
codingem.com › home › how to remove spaces from string in python
How to Remove Spaces from String in Python - codingem.com
July 10, 2025 - To remove white spaces, use string.whitespaces, which is a list of the types of blank spaces.
🌐
LabEx
labex.io › tutorials › python-how-to-strip-whitespace-from-a-python-string-417568
How to strip whitespace from a Python string | LabEx
In this Python tutorial, you have learned how to effectively remove whitespace from strings using built-in methods like .strip(), .lstrip(), and .rstrip(), as well as more advanced techniques such as regular expressions.
🌐
W3Schools
w3schools.in › python › examples › trim-whitespace-from-a-string
Python Program to Trim Whitespace From a String
" Sample string " --> "Sample string" " Sample string" --> "Sample string" "Sample string " --> "Sample string" "Sample string" --> "Sample string" The strip() function also removes tabs and newlines such as \n, \r, \t, \f, and spaces , so to remove only specific characters, specify it as an ...
🌐
Vultr
docs.vultr.com › python › examples › trim-whitespace-from-a-string
Python Program to Trim Whitespace From a String | Vultr Docs
December 25, 2024 - After applying lstrip(), you get the string "Leading space be gone!", with the spaces at the start removed. Employ the rstrip() method to trim whitespace from the end (right side) of the string only.