removing duplicate spaces from a string

Hans Brand Hans.Brand at BrandInnovators.com
Mon Aug 6 04:38:18 EDT 2001


Ron,

Split() and join() should do the trick.

>>> from string import *
>>> a = 'fiddle     faddle  pip  pop'
>>> join(a.split())
'fiddle faddle pip pop'
>>>

Greetings, Hans


"Ron Johnson" <ron.l.johnson at home.com> wrote in message
news:lvsb7.57979$oh1.21803460 at news2.rdc2.tx.home.com...
> Hello,
>
> Say I have the strings:
>   'foo   bar      snafu'
>   'fiddle     faddle  pip  pop'
>
> Are there any builtins that will allow me to compress the
> duplicate spaces out so that the files look like:
>   'foo bar snafu'
>   'fiddle faddle pip pop'
>
> I could iteratively apply string.replace, replacing '  ' with
> ' ', but that doesn't seem the optimum course.
>
> For anyone with VMS experience, I want to emulate the DCL lexical
> function F$EDIT("COMPRESS").
>
> Sincerely,
> Ron
>
> +------------------------------------------------------------+
> | Ron Johnson, Jr.        Home: ron.l.johnson at home.com       |
> | Jefferson, LA  USA      http://ronandheather.dhs.org       |
> |                                                            |
> | "Our computers and their computers are the same color. The |
> |  conversion should be no problem!"                         |
> |    Unknown                                                 |
> +------------------------------------------------------------+





More information about the Python-list mailing list