473,406 Members | 2,439 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 51355
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

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

Similar topics

5
by: qwweeeit | last post by:
Hi all, I need to limit as much as possible the lenght of a source line, stripping white spaces (except indentation). For example: .. . max_move and AC_RowStack.acceptsCards ( self,...
2
by: Malcolm Dew-Jones | last post by:
I am looking at xslt 1.0 and trying to understand if empty text nodes are supposed to be stripped or not as the default behaviour. 3.4 starts by listing rules for when white space is not stripped...
17
by: Stanimir Stamenkov | last post by:
Is it possible to make two inline elements to appear adjacent stripping any white space appearing in between in the source? Example: <span class="adj">1</span> <span class="adj">2</span>...
11
by: gopal srinivasan | last post by:
Hi, I have a text like this - "This is a message containing tabs and white spaces" Now this text contains tabs and white spaces. I want remove the tabs and white...
3
by: Prince | last post by:
I have some <RequiredFieldValidator> on my page and everything works fine except that there are lots of white spaces between the web server controls that are being validated. I've set the Display...
4
by: Andreas Prilop | last post by:
How many spaces should be displayed in A <span style="display:none">x</span> B between "A" and "B"? I notice that Mozilla displays one space and Internet Explorer (5 & 6) displays two spaces....
4
by: Nathan Sokalski | last post by:
I have two asp:ImageMaps in a table cell as follows: <table border="0" cellpadding="0" cellspacing="0" width="100%"> <tr valign="top"> <td align="center"> <asp:ImageMap ID="mapBanner"...
12
by: JA | last post by:
Is there a way to remove all the white space in the fields? I have been using Find-and-replace - looking for 2 or 3 or 4 or 10 spaces and replacing them with none. I don't want to replace single...
3
by: ayan4u | last post by:
well i need to deal with white spaces in charecter arrays... with static arrays its fine.. char ss; cin.getline(ss, sizeof ss); .... //deals with white spaces
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.