364,111 Members | 2059 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

How to strip of mulitiple white spaces.

shajias
P: 8
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
Share this Question
Share on Google+
5 Replies


shajias
P: 8
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
Expert 100+
P: 135
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
Expert 100+
P: 511
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
Expert Mod 2.5K+
P: 2,509
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
Expert 5K+
P: 5,734
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

Help answer this question



Didn't find the answer to your Python question?

You can also browse similar questions: Python python strip stripwhitespace python