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

  • Home
  • SEARCH
  • 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 887365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:13:04+00:00 2026-05-15T13:13:04+00:00

If I have a string this is a string How can I shorten it

  • 0

If I have a string

"this is   a    string"

How can I shorten it so that I only have one space between the words rather than multiple? (The number of white spaces is random)

"this is a string"
  • 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-15T13:13:05+00:00Added an answer on May 15, 2026 at 1:13 pm

    You could use string.split and " ".join(list) to make this happen in a reasonably pythonic way – there are probably more efficient algorithms but they won’t look as nice.

    Incidentally, this is a lot faster than using a regex, at least on the sample string:

    import re
    import timeit
    
    s = "this    is   a     string"
    
    def do_regex():
        for x in xrange(100000):
            a = re.sub(r'\s+', ' ', s)
    
    def do_join():
        for x in xrange(100000):
            a = " ".join(s.split())
    
    
    if __name__ == '__main__':
        t1 = timeit.Timer(do_regex).timeit(number=5)
        print "Regex: ", t1
        t2 = timeit.Timer(do_join).timeit(number=5)
        print "Join: ", t2
    
    
    $ python revsjoin.py 
    Regex:  2.70868492126
    Join:  0.333452224731
    

    Compiling this regex does improve performance, but only if you do call sub on the compiled regex, instead of passing the compiled form into re.sub as an argument:

    def do_regex_compile():
      pattern = re.compile(r'\s+')
      for x in xrange(100000):
        # Don't do this
        # a = re.sub(pattern, ' ', s)
        a = pattern.sub(' ', s)
    
    $ python revsjoin.py  
    Regex:  2.72924399376
    Compiled Regex:  1.5852200985
    Join:  0.33763718605
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have: String uri = @drawable/myresource.png; How can I load that in ImageView? this.setImageDrawable?
Problem: I have a string that looks like this: [1=>2,3,4][5=>6,7,8][9=>10,11,12][13=>14,15][16=>17,18] Question: How can you
If I have a string: string = <.one><.two>three<.four>[:five] , how can I manipulate this
I have this code for AES encryption, can some one verify that this code
I have string like this: G:\Projects\TestApp\TestWeb\Files\Upload\file.jpg How can I remove all text before Files
I have a string like this /blog/post1/ how can i get the post1 from
I have a string like this BRADI5G20430|BRADI5G20430.1||1 How can I replace the bar (single
I have this string in vb.net. I would appreciate if you can let me
If you have a string of 1,2,3,1,5,7 you can put this in an array
I can't get this to work.. I have an String which I want to

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.