Trimming Strings


str(len(badSentence.rstrip(' '))) print badSentence.lstrip('\t') print badParagraph.strip((' ?!\t'))

Common problems when parsing text are leftover characters at the beginning or end of the string. Python provides several strip methods to remove those characters. The strip([chrs]), lstrip([chrs]), and rstrip([chrs]) methods accept a list of characters as the only argument and return a new string with those characters trimmed from either the start, end, or both ends of the string.

Note

The strip will remove the specified characters from both the beginning and end of the string. The lstrip and rstrip methods remove the characters only from the beginning or end of the string, respectively.


import string badSentence = "\t\tThis sentence has problems.   " badParagraph = "\t\tThis paragraph \nhas even \     more \nproblems.!?   " #Strip trailing spaces print "Length = " + str(len(badSentence)) print "Without trailing spaces = " + \     str(len(badSentence.rstrip(' '))) #Strip tabs print "\nBad:\n" + badSentence print "\nFixed:\n" + badSentence.lstrip('\t') #Strip leading and trailing characters print "\nBad:\n" + badParagraph print "\nFixed:\n" + badParagraph.strip((' ?!\t'))


trim_str.py

Length = 32 Without trailing spaces = 29 Bad:                This sentence has problems. Fixed: This sentence has problems. Bad:                This paragraph has even more problems.!? Fixed: This paragraph has even more problems.


Output from trim_str.py code



Python Phrasebook(c) Essential Code and Commands
Python Phrasebook
ISBN: 0672329107
EAN: 2147483647
Year: N/A
Pages: 138
Authors: Brad Dayley

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net