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
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
how to do multi-line f string without the indentation mess

Why not split the f string?

from uuid import uuid4

a = (
  f"""{uuid4()}\n"""
  f"""and another line {uuid4()}""")

print(a)
More on reddit.com
🌐 r/Python
29
78
December 17, 2017
🌐
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.
🌐
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...
🌐
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…
Find elsewhere
🌐
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......
🌐
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.
🌐
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 ... 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.
🌐
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.
🌐
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 ...
🌐
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
The stripped text does not contain any whitespace characters. With the help of the rstrip() method, we can easily remove the trailing whitespace characters (spaces, tabs, and newline characters) from a string.