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

Copy>>> "  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):

Copy>>> "  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():

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

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

Copy>>> "".join("  hello  apple  ".split())
'helloapple'
Answer from Cédric Julien on Stack Overflow
🌐
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.
Discussions

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
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
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
I must be an idiot or something. Why can't I strip the comma's out of a string?
Maybe you're thinking of split()? test = '12,123,123,123' test2 = test.split(',') test2 ['12','123','123','123'] Concat the elements and the commas are gone :) More on reddit.com
🌐 r/learnpython
21
6
February 13, 2012
🌐
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
🌐
Java2Blog
java2blog.com › home › python › remove whitespace from string python
Remove whitespace from String Python - Java2Blog
February 24, 2021 - You can use String’s strip method to remove leading whitespace whitespaces from String in Python.
🌐
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))

🌐
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.
Find elsewhere
🌐
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 - import string before = "This \t is a \n test \n\n \t let's modify this" after = before.translate(str.maketrans("", "", string.whitespace)) print(before) print(after) ... RegEx or Regular Expression is like a Ctrl+F on steroids. You can use it to match patterns in a string. You can for example use regex to find phone numbers or email addresses from a text document. I am not going to go into more detail about regex, but here is a great article you can check. To use Regular Expressions in Python to remove white spaces from a string:
🌐
Sentry
sentry.io › sentry answers › python › remove whitespace from a string in python
Remove whitespace from a string in Python | Sentry
July 15, 2023 - To remove all whitespace characters ... import Python’s Regular Expressions module and use re.sub with "\s", a regular expression that will match any whitespace character....
🌐
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.
🌐
W3Schools
w3schools.com › python › ref_string_strip.asp
Python String strip() Method
Remove List Duplicates Reverse ... Plan Python Interview Q&A Python Bootcamp Python Training ... The strip() method removes any leading, and trailing whitespaces......
🌐
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
Mostly, the strip() function is of most use as it removes both leading and trailing whitespaces. ... String with 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…
🌐
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?
🌐
ItSolutionstuff
itsolutionstuff.com › post › python-remove-whitespace-from-start-and-end-of-string-exampleexample.html
Python Remove Whitespace from Start and End of String Example - ItSolutionstuff.com
October 30, 2023 - We will use python remove whitespace beginning and end. In this example, I will add myString variable with hello string. Then we will use strip() function to whitespace from start and end of string in python.
🌐
Educative
educative.io › answers › how-to-strip-whitespace-in-python
How to strip whitespace in Python
Line 2: We define a text with a string having whitespaces at the beginning. Line 5: Using the lstrip() method, we remove the whitespace and save the result in a new variable lstrip_text.
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-ways-to-remove-multiple-empty-spaces-from-string-list
Python - Ways to remove multiple empty spaces from string List - GeeksforGeeks
July 12, 2025 - Using re.sub() with regular expressions allows you to efficiently replace multiple spaces in a string with a single space. It’s a powerful method for cleaning up text data by handling patterns of unwanted whitespace. ... import re s = ["Hello world", " Python is great ", " Extra spaces here "] # Remove multiple spaces using regex li = [re.sub(r'\s+', ' ', string.strip()) for string in s] print(li)
🌐
Python Examples
pythonexamples.org › python-string-remove-spaces
Python String - Remove spaces
The re.sub() method returns a new string with all the white spaces replaced with empty string, effectively removing all the spaces from the string.
🌐
Python documentation
docs.python.org › 3 › library › stdtypes.html
Built-in Types — Python 3.14.5 documentation
The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a suffix; rather, all combinations of its values are stripped.