Loading video player…

Using the .strip() Method of Strings

00:00 The .strip() method of strings can be used to remove leading and trailing characters from an existing string. The syntax is as follows. cleaned_string equals the result of calling original_string.strip() with an optional chars argument.

00:14 chars should be a string of individual characters to be stripped. The outermost leading and trailing characters found in chars are removed until a character not found in chars is encountered.

00:24 The default value for chars is None, and when chars is None, all whitespace characters are stripped instead.

00:30 These are characters like spaces, tabs, and newlines, among others. And it’s important to note that the string methods we explore in this course return new strings, and do not modify or mutate existing strings in place.

00:45 Open up the REPL to look at some examples.

00:48 Start by defining a string with some extra spaces on both ends, called original_string. original_string equals space, space, space, Hello, Earth, space, space, space.

01:00 Since calling .strip() with no arguments will get rid of whitespace, go ahead and call it on original_string.

01:08 And the output is Hello, Earth! with all the extra spaces on both ends gone. Pretty straightforward. For a more complex example, create a multiline string using triple quotes called text.

01:20 text equals \n space \t another space, The truth is out there and an empty line, and a bunch more spaces and another \t and even more spaces. By the way, \n and \t are the escape characters for a newline and a tab, respectively.

01:41 Try calling text.strip()

01:44 and all the whitespace is gone. All that’s left is The truth is out there. You often see this kind of extra whitespace when you’re reading from a text file or scraping text from websites.

01:55 Okay, how about targeting a specific character? Create a string called review with the contents: This show is incredible and three exclamation marks.

02:05 Call review.strip() passing in a string with a single exclamation mark and see This show is incredible. All three instances of the exclamation mark have been stripped from the end of the string. .strip() can also be used to remove multiple characters from both ends of a string.

02:21 So what if the review were in Spanish instead? review equals three inverted exclamation marks, Esta

02:31 serie es increible, and three exclamation marks. Call review.strip() passing in a string with both an inverted and a regular exclamation mark.

02:41 The result is a much less enthusiastic Esta serie es increible,

02:46 and you native Spanish speakers out there, I apologize for my accent. Okay, for a final example, how about cleaning up extra characters on a user’s chosen username?

02:57 username equals _! couple more underscores, CS_Man and some more exclamation marks, and underscores thrown in at the end.

03:07 You can clean this up by calling username.strip() and passing in a string with an exclamation mark and an underscore in any order, username.strip('!_').

03:18 The result is CS_Man. And note how the single underscore remains. because it’s separated from the ends of the string by characters not included in your call to .strip(), it won’t be removed.

03:31 That’s pretty useful, right? But what if you only wanted to remove characters from the start or the end of a string and not both? Find out how in the next lesson.

Become a Member to join the conversation.