Pythonic way to trim and keep leading and trailing whitespace

Shashwat Anand anand.shashwat at gmail.com
Tue Mar 23 18:50:25 EDT 2010


regex is not goto that you should always avoid using it. It have its own
use-case, here regex solution is intuitive although @emile have done this
for you via string manpulation.

On Wed, Mar 24, 2010 at 4:09 AM, Daniel Chiquito
<daniel.chiquito at gmail.com>wrote:

> As far as I know, I don't think there is anything that strips it and
> returns the material that was stripped. Regex's would be your best bet.
>
> Daniel
>
> On Tue, Mar 23, 2010 at 6:09 PM, <python at bdurham.com> wrote:
>
>> 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.
>>
>> 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
>>
>>
>>
>> --
>>
>> http://mail.python.org/mailman/listinfo/python-list
>>
>>
>
>
> --
> ~
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100324/0d8da103/attachment.html>


More information about the Python-list mailing list