Stripping new lines from strings?

Matthew Schinckel matt at null.net
Thu Aug 24 10:17:00 EDT 2000


In message <31575A892FF6D1118F5800600846864D5B104E at intrepid>, Simon Brunning
wrote:
> Speed isn't the problem - the problem with this is that it strips stuff out
> of the middle as well as the ends. So:
> 
> >>> brian = '***words***words***'
> >>> string.join(string.split(brian, '*'), '')
> 'wordswords'
> 
> But if split had a second argument, you could do:
> >>>string.strip(brian, '*')
> 'words***words'
> 

Okay, I guess I misunderstood.  I didn't really realize we were only wanting to
strip from the start/end.  (Hey, I suppose string.replace(str, '\n', '') does
exactly what my other code did).

What about using re:

re.sub("\n$", '', str)

(Or something similar?)

-- 
Matthew Schinckel     <matt at null.net>




More information about the Python-list mailing list