[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Re: Pythonic way to trim and keep leading and trailing whitespace

Emile van Sebille

3/23/2010 10:35:00 PM

On 3/23/2010 3:09 PM python@bdurham.com said...
> I'm looking for a pythonic way to trim and keep leading
> whitespace in a string.
>
> Use case: I have a bunch of text strings with various amounts of
> leading and trailing whitespace (spaces and tabs). I want to grab
> the leading and trailing whitespace, save it, surround the
> remaining text with html tags, and then add back the leading and
> trailing whitespace.

I'd do it this way:

target = ' spam and eggs '
stripped = target.strip()
replaced = target.replace(stripped,"<html>%s</html>" % stripped)

HTH,

Emile


>
> The only solution I can think of is regex, and that makes me
> think of the 2 proverbial problems that come with that :)
>
> Is there a 'better' solution than regex for this scenario? (Seems
> like this would be a common type of string processing).
>
> Thanks,
> Malcolm
>
>