469,582 Members | 2,388 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,582 developers. It's quick & easy.

How to strip of mulitiple white spaces.

Hi all,

Suppose I am having a string like this.

mystr = " I have five spaces after this and 3 spaces after this and 10 spaces after this. How to remove this muliple whitespaces "

I just want to remove this multiple whitespaces and need the output like this

I have five spaces after this and 3 spaces after this and 10 spaces after this. How to remove this muliple whitespaces

Can anyone help me out please?
Jan 19 '07 #1
5 51003
Since I couldnt retain the format which I wanted in the mail, I am rewording my question.

Hi all,

I am having adjacent multiple white spaces(both leading, trailing and in the middle) in a string. I want to strip off all the multiple white spaces and need to substitute multiple whitespaces by one white space there. Is there any way for that.?

I know that leading and trailing whitespaces can be stripped by rstrip and lstrip. But I couldnt do anythign with multiple whitespaces in the middle of a string

Can anybody help me out please?

~Sas
Jan 19 '07 #2
dshimer
136 Expert 100+
I'm not sure this is particularly elegant, but it was my first thought.
Expand|Select|Wrap|Line Numbers
  1. origstrg='there   are some  places   with many      spaces'
  2. newstrg=''
  3. for word in origstrg.split():
  4.     newstrg=newstrg+' '+word
  5. print newstrg
  6.  
which should come up as...
there are some places with many spaces
Jan 19 '07 #3
ghostdog74
511 Expert 256MB
Since I couldnt retain the format which I wanted in the mail, I am rewording my question.

Hi all,

I am having adjacent multiple white spaces(both leading, trailing and in the middle) in a string. I want to strip off all the multiple white spaces and need to substitute multiple whitespaces by one white space there. Is there any way for that.?

I know that leading and trailing whitespaces can be stripped by rstrip and lstrip. But I couldnt do anythign with multiple whitespaces in the middle of a string

Can anybody help me out please?

~Sas


1) you can strip off trailing and leading whitespaces using strip(). So there's no need for lstrip,rstrip
eg
Expand|Select|Wrap|Line Numbers
  1. >>> s = "    a string with leading and trailing spaces    "
  2. >>> s.strip()
  3. 'a string with leading and trailing spaces'
  4.  
2) if you know the number of multiple white spaces in the middle, eg 2 white spaces, you can use replace() , eg for 2 white spaces
Expand|Select|Wrap|Line Numbers
  1. >>> s = "a string  with multiple  white spaces"
  2. >>> s.replace("  "," ")
  3. 'a string with multiple white spaces'
  4. >>> 
  5.  
3) if you don't know the number of white spaces in the middle, you can use split(), which i think is most straight forward :)
Expand|Select|Wrap|Line Numbers
  1. >>> s = "a string  with multiple    white          spaces"
  2. >>> s.split()
  3. ['a', 'string', 'with', 'multiple', 'white', 'spaces']
  4. >>> ' '.join(s.split())
  5. 'a string with multiple white spaces'
  6.  
4) you can also use regular expression module eg
Expand|Select|Wrap|Line Numbers
  1. >>> import re
  2. >>> s = "a string  with multiple    white          spaces"
  3. >>> re.sub("\s+" , " ", s)
  4. 'a string with multiple white spaces'
  5.  
Jan 19 '07 #4
bvdet
2,851 Expert Mod 2GB
ghostdog,

Your option #3 also eliminates the leading and trailing spaces, therefore may be the best solution.
Expand|Select|Wrap|Line Numbers
  1. >>> s = "      a string  with multiple    white          spaces    "
  2. >>> " ".join(s.split())
  3. 'a string with multiple white spaces'
The string method join is very efficient.
Jan 19 '07 #5
bartonc
6,596 Expert 4TB
ghostdog,

Your option #3 also eliminates the leading and trailing spaces, therefore may be the best solution.
Expand|Select|Wrap|Line Numbers
  1. >>> s = "      a string  with multiple    white          spaces    "
  2. >>> " ".join(s.split())
  3. 'a string with multiple white spaces'
The string method join is very efficient.
That's exactly how I'd do it.
Jan 19 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by qwweeeit | last post: by
2 posts views Thread by Malcolm Dew-Jones | last post: by
17 posts views Thread by Stanimir Stamenkov | last post: by
11 posts views Thread by gopal srinivasan | last post: by
3 posts views Thread by Prince | last post: by
4 posts views Thread by Andreas Prilop | last post: by
4 posts views Thread by Nathan Sokalski | last post: by
12 posts views Thread by JA | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.