can it be shorter?

Roland Mueller roland.em0001 at googlemail.com
Mon Jun 8 08:25:51 EDT 2009


2009/6/8 Aaron Brady <castironpi at gmail.com>

> On Jun 7, 6:13 pm, Paul Rubin <http://phr...@NOSPAM.invalid> wrote:
> > Aaron Brady <castiro... at gmail.com> writes:
> > > url+= { '/': '' }.get( url[ -1 ], '/' )
> >
> > > Shorter is always better.
> >
> > url = url.rstrip('/') + '/'
>
> I was joking.  Sheesh.
> --
> http://mail.python.org/mailman/listinfo/python-list
>

my two cents: a solution based on regex: the pattern replaces all slashes at
the end of the string with a single one. The * usage matches also the empty
group of slashes (example s2)

>>> print s1
aaaaa/
>>> print s2
bbbb
>>> re.sub('[/]*$','/', s1)
'aaaaa/'
>>> re.sub('[/]*$','/', s2)
'bbbb/'

-Roland
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090608/5e57cb70/attachment.html>


More information about the Python-list mailing list