Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1076293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:23:37+00:00 2026-05-16T21:23:37+00:00

How do I strip all the spaces in a python string? For example, I

  • 0

How do I strip all the spaces in a python string? For example, I want a string like strip my spaces to be turned into stripmyspaces, but I cannot seem to accomplish that with strip():

>>> 'strip my spaces'.strip()
'strip my spaces'
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T21:23:38+00:00Added an answer on May 16, 2026 at 9:23 pm

    Taking advantage of str.split’s behavior with no sep parameter:

    >>> s = " \t foo \n bar "
    >>> "".join(s.split())
    'foobar'
    

    If you just want to remove spaces instead of all whitespace:

    >>> s.replace(" ", "")
    '\tfoo\nbar'
    

    Premature optimization

    Even though efficiency isn’t the primary goal—writing clear code is—here are some initial timings:

    $ python -m timeit '"".join(" \t foo \n bar ".split())'
    1000000 loops, best of 3: 1.38 usec per loop
    $ python -m timeit -s 'import re' 're.sub(r"\s+", "", " \t foo \n bar ")'
    100000 loops, best of 3: 15.6 usec per loop
    

    Note the regex is cached, so it’s not as slow as you’d imagine. Compiling it beforehand helps some, but would only matter in practice if you call this many times:

    $ python -m timeit -s 'import re; e = re.compile(r"\s+")' 'e.sub("", " \t foo \n bar ")'
    100000 loops, best of 3: 7.76 usec per loop
    

    Even though re.sub is 11.3x slower, remember your bottlenecks are assuredly elsewhere. Most programs would not notice the difference between any of these 3 choices.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to strip all spaces that are not between two letters : $string
Possible Duplicate: How do I strip all spaces out of a string in PHP?
I'm trying to extract all words from a string into an array, but i
I want to strip all spaces that are not between two words? The script
How can I strip / remove all spaces of a string in PHP? I
Hi i need to strip all spaces and newlines from a string in javascript...
I'd like to take a string, strip it of all non-alphanumeric characters and convert
I have a $text to strip off all non-alphanumeric chars, replace multiple white spaces
How do I strip all punctuation from a string in vb.net? I really do
I need to strip all xml tags from an xml document, but keep the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.