<p>All of the explanations in the other answers are fine for <code>strip()</code>, 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 <code>strip</code> will do - it's a very sensible operation, and not hard to understand if you have the background knowledge.</p>
<p>But in my experience, <code>strip()</code> is often introduced (often <em>must</em> 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.</p>
<p>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.</p>
<p>Then we talk (only very briefly!) about strings as arrays of such characters, and wind our way back to strip.</p>
<p>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 <code>strip()</code> creates a sort of &quot;cleaned up&quot; 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.</p>

CC BY-SA 4.0
2023-06-15T15:40:33.617Z
2023-06-15T15:40:33.617Z
Ben I.
<p>Start with why:</p>
<p>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).</p>
<p>Then, it would be nice to have a way to solve this problem.</p>
<p>Then, Oh look, we have a way to solve this problem.</p>

CC BY-SA 4.0
2023-07-15T20:53:30.263Z
2023-07-16T11:06:43.320Z
2023-07-16T11:06:43.320Z
ctrl-alt-delor
<p>I would introduce this with a problem/relatable example, such as:</p>
<p>I'd make a little text-based program that printed out:</p>
<pre><code>Welcome to Birdr, your favorite social media site for bird-watching.
Would you like to make an account?
&gt; Yes
What is your first name?
&gt; [I'd ask the students for a fake first name]
What is your last name?
&gt; [I'd ask the students for a fake last name]
Welcome, [First Last]
</code></pre>
<p>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.</p>
<p>Then, once the program had run, I'd ask the class the following questions:</p>
<ul>
<li>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.</li>
<li>Look at this weird formatting (e.g., <code>Welcome,   Harry      Potter </code>), why is that happening? (I typed extra spaces around both words)</li>
</ul>
<p>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 <code>strip()</code> function. <code>strip()</code> gets rid of extra &quot;whitespace&quot; before and after a string. Whitespace is stuff like spaces.</p>
<p>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.</p>
<p>Finally, I'd ask the class to brainstorm other situations that <code>strip()</code> might be useful.</p>

CC BY-SA 4.0
2023-06-24T00:05:21.360Z
2023-06-24T00:05:21.360Z
Green Griffin
<p><code>strip</code> removes characters from the beginning and end of a string</p>
<p>The removed characters can be passed as the first argument to <code>strip</code>.  If none are specified <code>strip</code> will remove whitespace (<code>\n</code> <code>\t</code> <code>\r</code> and space usually).</p>
<p><code>strip</code> does <em>NOT</em> remove characters from the middle of a string.</p>
<pre><code>Python 3.10.6 (main, May 29 2023, 11:10:38) [GCC 11.3.0] on linux
Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.
&gt;&gt;&gt; print ('x y'.strip())
x y
&gt;&gt;&gt; print ('x y'.strip(' '))
x y
&gt;&gt;&gt; print (' x y'.strip(' '))
x y
&gt;&gt;&gt; print (' x y '.strip(' '))
x y
&gt;&gt;&gt; print (('\&quot;{0}\&quot;').format(' x y '.strip(' ')))
&quot;x y&quot;
&gt;&gt;&gt; print (('\&quot;{0}\&quot;').format('x y '.strip(' ')))
&quot;x y&quot;
&gt;&gt;&gt; print (('\&quot;{0}\&quot;').format('x\ty '.strip(' ')))
&quot;x  y&quot;
&gt;&gt;&gt; print (('\&quot;{0}\&quot;').format('x\ty '.strip()))
&quot;x  y&quot;
&gt;&gt;&gt; 'www.example.com'.strip('am')
'www.example.co'
&gt;&gt;&gt; 'www.example.com'.strip('xa')
'www.example.com'
&gt;&gt;&gt; 'www.example.com'.strip('ple')
'www.example.com'
&gt;&gt;&gt; 
</code></pre>

CC BY-SA 4.0
2023-06-12T16:28:01.250Z
2023-06-12T16:28:01.250Z
tripleo
<p>The phone number <code>&quot;(303) 719 -7210⎵⎵⎵⎵&quot;</code> has some spaces <code>⎵</code> on the right-hand side.</p>
<p>Most computers do not like spaces.</p>
<p>We can delete the spaces from the right-hand side.</p>
<p>Be careful, <code>⎵</code> is not a space; we only write <code>⎵</code> in this example because <code>⎵</code> is easier to see with your eyes than an actual space character</p>
<p>space is <code>chr(32)</code> or character number 32 in the ASCII character set.</p>
<pre class="lang-python prettyprint-override"><code># `iphone` stands for `input phone number`   
iphone = &quot;(303) 719-7210    &quot;

# output phone number
ophone = iphone.strip()   

# ophone = &quot;(303) 719-7210&quot;
</code></pre>
<div class="s-table-container">
<table class="s-table">
<thead>
<tr>
<th>INPUT</th>
<th>OUTPUT</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>⎵⎵⎵0. Biodegradable Bowl\n\r</code></td>
<td><code>0. Biodegradable Bowl</code></td>
</tr>
<tr>
<td><code>⎵Alicia\rSanz\r</code></td>
<td><code>Alicia\rSanz</code></td>
</tr>
<tr>
<td><code>\nAna\nFigueroa\t</code></td>
<td><code>Ana\nFigueroa</code></td>
</tr>
<tr>
<td><code>\tAriadna\tManzanares </code></td>
<td><code>Ariadna\tManzanares</code></td>
</tr>
<tr>
<td><code>\rBriana\nCoronil\r</code></td>
<td><code>Briana\nCoronil</code></td>
</tr>
<tr>
<td><code>\nDrp\nKilling\t</code></td>
<td><code>Drp\nKilling</code></td>
</tr>
<tr>
<td><code>\rElding\tDawn\r</code></td>
<td><code>Elding\tDawn</code></td>
</tr>
<tr>
<td><code>\rGefa Fortune </code></td>
<td><code>Gefa Fortune</code></td>
</tr>
<tr>
<td><code>\nGaren Beak\r</code></td>
<td><code>Garen Beak</code></td>
</tr>
<tr>
<td><code>\rHariett Candalia⎵⎵⎵</code></td>
<td><code>Hariett Candalia</code></td>
</tr>
<tr>
<td><code>⎵Inmaculada\nFragan </code></td>
<td><code>Inmaculada\nFragan</code></td>
</tr>
<tr>
<td><code>\nJuana\tPrats\n</code></td>
<td><code>Juana\tPrats</code></td>
</tr>
<tr>
<td><code>⎵Laura\tAgramonte </code></td>
<td><code>Laura\tAgramonte</code></td>
</tr>
<tr>
<td><code>\tMara\rJos\rSenz </code></td>
<td><code>Mara\rJos\rSenz</code></td>
</tr>
<tr>
<td><code>\nMetnain\nMetnar\nGlory </code></td>
<td><code>Metnain\nMetnar\nGlory</code></td>
</tr>
<tr>
<td><code>\nOctavia Sarmiento\t</code></td>
<td><code>Octavia Sarmiento</code></td>
</tr>
<tr>
<td><code>\tSeii\rSeir\rEnchantment\r</code></td>
<td><code>Seii\rSeir\rEnchantment</code></td>
</tr>
<tr>
<td><code>⎵Sk Wekan⎵</code></td>
<td><code>Sakie Wekan</code></td>
</tr>
<tr>
<td><code>⎵Silvia\rSanders\r</code></td>
<td><code>Sivlia\rSanders</code></td>
</tr>
<tr>
<td><code>\tTal\rConversation\n</code></td>
<td><code>Tal\rConversation</code></td>
</tr>
<tr>
<td><code>⎵Yolanda\nNavarro </code></td>
<td><code>Yolanda\nNavarro</code></td>
</tr>
</tbody>
</table>
</div>
CC BY-SA 4.0
2023-06-06T14:31:21.643Z
"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.
Solomon Slow
CC BY-SA 4.0
2023-06-25T18:52:22.923Z
@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".
Samuel Muldoon
CC BY-SA 4.0
2023-06-25T18:55:39.837Z
@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`
Samuel Muldoon
CC BY-SA 4.0
2023-06-25T19:25:50.013Z
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.
Solomon Slow
CC BY-SA 4.0
2023-06-06T01:29:24.897Z
2023-08-29T13:29:42.980Z
2023-08-29T13:29:42.980Z
Samuel Muldoon
<p>I suggest</p>
<blockquote>
<p>Strings can contain characters that you can't see such as spaces between words, or marking the end of a line. <code>str.strip</code> 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.</p>
</blockquote>
<p>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.</p>

CC BY-SA 4.0
2023-06-14T11:28:50.757Z
2023-06-14T11:28:50.757Z
Jack Aidley
<p>When teaching python to new students in middle school (or any age group), the students sometimes find the <a href="https://docs.python.org/3/library/stdtypes.html?highlight=str%20strip#str.startswith" rel="nofollow noreferrer"><strong>python documentation</strong></a> to be a bit overwhelming.</p>
<p>What is a short, easy-to-understand description of <code>str.strip()</code> for strings, which a person with limited experience with computer programming would understand?</p>

<resource-request><lesson-ideas><introductory-lesson>
What Informal Description Might Help Beginning Students Understand Python's `str.strip()` method?
# What Informal Description Might Help Beginning Students Understand Python's `str.strip()` method?

(Asked by: username_0 on 2023-06-06)

When teaching python to new students in middle school (or any age group), the students sometimes find the [**python documentation**](https://docs.python.org/3/library/stdtypes.html?highlight=str%20strip#str.startswith) 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?

**Tags:** resource-request, lesson-ideas, introductory-lesson


---

## 6 answers

### Answer by username_1 on 2023-06-15. Score: 3

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.


---

### Answer by username_2 on 2023-07-15. Score: 2

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.


---

### Answer by username_3 on 2023-06-24. Score: 2

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.


---

### Answer by username_4 on 2023-06-12. Score: 1

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.co'
>>> 'www.example.com'.strip('xa')
'www.example.com'
>>> 'www.example.com'.strip('ple')
'www.example.com'
>>>


---

### Answer by username_0 on 2023-06-06. Score: 1

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"





INPUT
OUTPUT




⎵⎵⎵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

> **Comments:**
> **username_5**: "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.  
> **username_0**: @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".  
> **username_0**: @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`  
> **username_5**: 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.  

---

### Answer by username_6 on 2023-06-14. Score: 1

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.


---

