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. Sometimes, we only need to remove spaces from the start and end of a string while leaving the inner spaces untouched.
Published   July 11, 2025
Discussions

How to Remove Spaces From a String in Python (Complete 2026 Guide)
Need to remove spaces in Python? Get one-line solutions, edge-case handling, and when to use each method. More on accuweb.cloud
🌐 accuweb.cloud
1
December 1, 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 whitespaces from a string in Python?
Hi, I want to know How to remove whitespaces from a string in Python More on discuss.python.org
🌐 discuss.python.org
2
0
March 8, 2021
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
🌐
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 - 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.
🌐
W3Schools
w3schools.com › PYTHON › ref_string_strip.asp
Python String strip() Method
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Training · ❮ String Methods · Remove spaces at the beginning and at the end of the string: txt = " banana " x = txt.strip() print("of all fruits", x, "is my favorite") Try it Yourself » ·
🌐
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
🌐
DataCamp
datacamp.com › tutorial › python-trim
How to Trim a String in Python: Three Different Methods | DataCamp
February 16, 2025 - By default, these methods remove all types of whitespace, including spaces, tabs (\t), and newlines (\n). For example: text = "\t\n Python Programming \n\t" trimmed_text = text.strip() print(trimmed_text) # Output: "Python Programming" If you ...
🌐
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.
🌐
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
🌐
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…
🌐
Sololearn
sololearn.com › en › Discuss › 2055633 › pls-how-do-i-remove-white-space-between-two-words-in-python
Pls how do i remove white space between two words in ...
November 1, 2019 - Sololearn is the world's largest community of people learning to code. With over 25 programming courses, choose from thousands of topics to learn how to code, brush up your programming knowledge, upskill your technical ability, or stay informed about the latest trends.
🌐
CloudSigma
blog.cloudsigma.com › removing-spaces-in-python
Removing Spaces in Python — CloudSigma
May 16, 2023 - With the help of the strip() function, we can remove the leading and tailing characters of a string. The following code demonstrates using the strip() function on the variable s: ... Note that the strip() function removes all the leading and ...
🌐
YouTube
youtube.com › shorts › CmFUcIrQ0y8
Removing All Whitespace from a Python String! #python #coding - YouTube
To remove all whitespace from a Python string utilize the "replace" function.1. s = " te s t"2. s = s.replace(" ", "")
Published   February 4, 2025
🌐
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.
🌐
Instanceofjava
instanceofjava.com › 2021 › 07 › remove-spaces-from-string-python.html
remove spaces from string python - InstanceOfJava
July 7, 2021 - There are three functions that remove the whitespace between the strings, they are: strip(), rstrip(), lstrip() are the three functions having the same meaning but some differences.strip(): It removes the whitespace at the beginning and end.
🌐
freeCodeCamp
freecodecamp.org › news › python-strip-how-to-trim-a-string-or-line
Python strip() – How to Trim a String or Line
January 12, 2022 - Use the .rstrip() method to remove whitespace and characters only from the end of a string. If you want to learn more about Python, check out freeCodeCamp's Python Certification. You'll start learning in an interacitve and beginner-friendly way.
🌐
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
Often, we need to remove only extra spaces, such as those at the start or end of a string. Python provides several similar methods for this purpose: strip() removes spaces from both the string's start and end.
🌐
Python
docs.python.org › 3 › library › string.html
string — Common string operations
Split the argument into words using str.split(), capitalize each word using str.capitalize(), and join the capitalized words using str.join(). If the optional second argument sep is absent or None, runs of whitespace characters are replaced by a single space and leading and trailing whitespace are removed, otherwise sep is used to split and join the words.
🌐
30 Seconds of Code
30secondsofcode.org › home › python › trim whitespace
How do I trim whitespace from a string in Python? - 30 seconds of code
December 13, 2021 - Whitespace characters are the space ( ), tab (\t), newline (\n), and carriage return characters (\r). Here are 3 different methods to trim whitespace from a string in Python. Use the str.strip() method to remove whitespace characters from both ...