Python String Strip Method

title: String Strip Method

String Strip Method

There are three options for stripping characters from a string in Python, lstrip(), rstrip() and strip().

Each will return a copy of the string with characters removed, at from the beginning, the end or both beginning and end. If no arguments are given the default is to strip whitespace characters.


>>> string = ' Hello, World! ' >>> strip_beginning = string.lstrip() >>> strip_beginning 'Hello, World! ' >>> strip_end = string.rstrip() >>> strip_end ' Hello, World!' >>> strip_both = string.strip() >>> strip_both 'Hello, World!'

An optional argument can be provided as a string containing all characters you wish to strip.

>>> url = '' >>> url.strip('w./') ''

However, do notice that only the first . got stripped from the string. This is because the strip function only strips the argument characters that lie at the left or rightmost. Since w comes before the first . they get stripped together, whereas ‘com’ is present in the right end before the . after stripping /

More Information:

String methods documentation.

This article needs improvement. You can help improve this article. You can also write similar articles and help the community.