Strip Characters in Python

Strip Methods

Python strings have the strip(), lstrip(), rstrip() methods for removing any character from both ends of a string. If the characters to be removed are not specified then white-space will be removed

strip() #removes from both ends

lstrip() #removes leading characters (Left-strip)

rstrip() #removes trailing characters (Right-strip)

Examples

spacious = "   xyz   "
print spacious.strip()

spacious = "   xyz   "
print spacious.lstrip()

spacious =  "xyz   "
print spacious.rstrip()

Leave a Reply

Your email address will not be published. Required fields are marked *