When teaching python to new students in middle school (or any age group), the students sometimes find the python documentation to be a bit overwhelming.

What is a short, easy-to-understand description of str.strip() for strings, which a person with limited experience with computer programming would understand?


6 Answers 6


All of the explanations in the other answers are fine for strip(), and will do very well for students who have a good intuition for what strings and characters are. But in truth, for students with good intuitions about those concepts, essentially any explanation of strip will do - it's a very sensible operation, and not hard to understand if you have the background knowledge.

But in my experience, strip() is often introduced (often must be introduced, because it is needed for some lab assignment or another) prior to all of the students having a firm grasp on what a character is, and on what a string is.

So we proceed! If I give a reasonable explanation of the function and find that some of my students are still somewhat confused, I move on to what characters are, starting with the fact that everything that comes from the keyboard, including enter, are characters, and that there are even characters that don't use any space at all.

Then we talk (only very briefly!) about strings as arrays of such characters, and wind our way back to strip.

I would caution newer teachers to temper their expectations at this point in our journey. There are genuinely a lot of small abstractions built up in that short conversation, and it may take students some time to absorb all of them, so comprehension may not really be 100% yet. But students should absolutely come out with the idea that strip() creates a sort of "cleaned up" version of the string, but with the cleaning only done on the outsides. The rest may take a bit longer to catch up, and that is okay. Building firm mental models takes time.


I would introduce this with a problem/relatable example, such as:

I'd make a little text-based program that printed out:

Welcome to Birdr, your favorite social media site for bird-watching.
Would you like to make an account?
> Yes
What is your first name?
> [I'd ask the students for a fake first name]
What is your last name?
> [I'd ask the students for a fake last name]
Welcome, [First Last]

When typing in the students' suggestions for names, I'd pretend to be having difficulty typing. I'd make a big show of hunting for letters, struggling with capitalization, etc. and I'd make sure to leave several spaces at the beginning and end of words.

Then, once the program had run, I'd ask the class the following questions:

  • How is this working? What have we learned about so far that's being used here? (Printing, input, variables, strings). I'd show them the source code at this point, which would have been hidden until now.
  • Look at this weird formatting (e.g., Welcome, Harry Potter ), why is that happening? (I typed extra spaces around both words)

Often, when we're getting user input, people will make mistakes like adding extra spaces before or after words. Luckily, there's a way to clean that up! We can use Python's strip() function. strip() gets rid of extra "whitespace" before and after a string. Whitespace is stuff like spaces.

I'd show adding it in and then running the program again. Then we'd talk about the difference between using it on each of the inputted variables vs using it on the concatenated string and we'd see an example of each. I'd ask the students to point out the difference and ask them to hypothesize why they were different.

Finally, I'd ask the class to brainstorm other situations that strip() might be useful.


Start with why:

e.g. user input sometimes has leading and trailing space. The user does not see this space. To the user this is equivalent. Therefore it should be equivalent (to there being no leading and trailing space).

Then, it would be nice to have a way to solve this problem.

Then, Oh look, we have a way to solve this problem.


strip removes characters from the beginning and end of a string

The removed characters can be passed as the first argument to strip. If none are specified strip will remove whitespace (\n \t \r and space usually).

strip does NOT remove characters from the middle of a string.

Python 3.10.6 (main, May 29 2023, 11:10:38) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print ('x y'.strip())
x y
>>> print ('x y'.strip(' '))
x y
>>> print (' x y'.strip(' '))
x y
>>> print (' x y '.strip(' '))
x y
>>> print (('\"{0}\"').format(' x y '.strip(' ')))
"x y"
>>> print (('\"{0}\"').format('x y '.strip(' ')))
"x y"
>>> print (('\"{0}\"').format('x\ty '.strip(' ')))
"x  y"
>>> print (('\"{0}\"').format('x\ty '.strip()))
"x  y"
>>> 'www.example.com'.strip('am')
>>> 'www.example.com'.strip('xa')
>>> 'www.example.com'.strip('ple')

I suggest

Strings can contain characters that you can't see such as spaces between words, or marking the end of a line. str.strip will remove these characters if they occur at the start or end of the string but not in the middle. This is useful because often when you chop up a longer string into words or lines you are left with these invisible characters around the pieces that you want.

This introduces the idea of whitespace, explains what strip does, and explains why you may want to use it, without getting bogged down in technical details that are not necessary to understand at this stage.


The phone number "(303) 719 -7210⎵⎵⎵⎵" has some spaces on the right-hand side.

Most computers do not like spaces.

We can delete the spaces from the right-hand side.

Be careful, is not a space; we only write in this example because is easier to see with your eyes than an actual space character

space is chr(32) or character number 32 in the ASCII character set.

# `iphone` stands for `input phone number`   
iphone = "(303) 719-7210    "

# output phone number
ophone = iphone.strip()   

# ophone = "(303) 719-7210"
⎵⎵⎵0. Biodegradable Bowl\n\r 0. Biodegradable Bowl
⎵Alicia\rSanz\r Alicia\rSanz
\nAna\nFigueroa\t Ana\nFigueroa
\tAriadna\tManzanares Ariadna\tManzanares
\rBriana\nCoronil\r Briana\nCoronil
\nDrp\nKilling\t Drp\nKilling
\rElding\tDawn\r Elding\tDawn
\rGefa Fortune Gefa Fortune
\nGaren Beak\r Garen Beak
\rHariett Candalia⎵⎵⎵ Hariett Candalia
⎵Inmaculada\nFragan Inmaculada\nFragan
\nJuana\tPrats\n Juana\tPrats
⎵Laura\tAgramonte Laura\tAgramonte
\tMara\rJos\rSenz Mara\rJos\rSenz
\nMetnain\nMetnar\nGlory Metnain\nMetnar\nGlory
\nOctavia Sarmiento\t Octavia Sarmiento
\tSeii\rSeir\rEnchantment\r Seii\rSeir\rEnchantment
⎵Sk Wekan⎵ Sakie Wekan
⎵Silvia\rSanders\r Sivlia\rSanders
\tTal\rConversation\n Tal\rConversation
⎵Yolanda\nNavarro Yolanda\nNavarro
  • 1
    $\begingroup$ "Most computers do not like spaces" is an absurd assertion. But, I suppose it's easier to say that than to talk about reducing imported data to some canonical form that the rest of the application can easily deal with. $\endgroup$ Commented Jun 6, 2023 at 14:31
  • $\begingroup$ @SolomonSlow When I wrote that "most computers do not like spaces" I meant that there exist websites which display error messages when people type in their first name, phone number, street address, or other information with spaces. For example, if a person purchases or buys a stainless steel rain-proof or water-proof bicycle chain over the internet, and the person types (303) 719 -7210⎵⎵⎵⎵ or Oklahoma City⎵⎵ then the webpage will display a message similar to "Illegal character detected in phone number". $\endgroup$ Commented Jun 25, 2023 at 18:52
  • $\begingroup$ @SolomonSlow When I wrote that "most computers do not like spaces" I meant that statement to embody the idea that many web-designers forget to trim-off leading and trailing white space characters from user-input. There exist web-designers who create websites which say "illegal character in City field" instead of simply copy and pasting code from GitHub which would automatically delete leading and trailing white-space characters. There exist web-designers who write code to annoy end-users rather than help the person using the computer. When computers trim Lakehill⎵⎵ the result is Lakehill $\endgroup$ Commented Jun 25, 2023 at 18:55
  • 3
    $\begingroup$ I've met people who have a hard time seeing the difference between a web browser and a web site, between a database and a data entry form or a search form, between an e-mail client and and e-mail service and its web interface and their e-mail account, between any of the above and, "the computer." Some of those people are students in "computer science" classes. I'm not so careful about my language when I'm talking to professionals in the industry, but when I'm talking to those who are trying to understand new ideas, I try to describe those ideas with precision. $\endgroup$ Commented Jun 25, 2023 at 19:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.