8000 Be more explicit about replace examples · Gopal-py/gk-learn-python@4a4dec4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4a4dec4

Browse files
committed
Be more explicit about replace examples
1 parent 857b14c commit 4a4dec4

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

notebooks/beginner/html/strings.html

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12015,13 +12015,22 @@ <h2 id="str.replace()"><code>str.replace()</code><a class="anchor-link" href="#s
1201512015
</div>
1201612016
</div>
1201712017

12018+
</div>
12019+
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
12020+
</div>
12021+
<div class="inner_cell">
12022+
<div class="text_cell_render border-box-sizing rendered_html">
12023+
<p>This will not modify <code>my_string</code> because replace is not done in-place.</p>
12024+
12025+
</div>
12026+
</div>
1201812027
</div>
1201912028
<div class="cell border-box-sizing code_cell rendered">
1202012029
<div class="input">
1202112030
<div class="prompt input_prompt">In&nbsp;[7]:</div>
1202212031
<div class="inner_cell">
1202312032
<div class="input_area">
12024-
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">my_string</span> <span class="o">=</span> <span class="n">my_string</span><span cl 10000 ass="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;a&#39;</span><span class="p">,</span> <span class="s1">&#39;?&#39;</span><span class="p">)</span>
12033+
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">my_string</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;a&#39;</span><span class="p">,</span> <span class="s1">&#39;?&#39;</span><span class="p">)</span>
1202512034
<span class="nb">print</span><span class="p">(</span><span class="n">my_string</span><span class="p">)</span>
1202612035
</pre></div>
1202712036

@@ -12039,14 +12048,23 @@ <h2 id="str.replace()"><code>str.replace()</code><a class="anchor-link" href="#s
1203912048

1204012049

1204112050
<div class="output_subarea output_stream output_stdout output_text">
12042-
<pre>Python is my f?vorite progr?mming l?ngu?ge!
12051+
<pre>Python is my favorite programming language!
1204312052
</pre>
1204412053
</div>
1204512054
</div>
1204612055

1204712056
</div>
1204812057
</div>
1204912058

12059+
</div>
12060+
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
12061+
</div>
12062+
<div class="inner_cell">
12063+
<div class="text_cell_render border-box-sizing rendered_html">
12064+
<p>You have to store the return value of <code>replace</code> instead.</p>
12065+
12066+
</div>
12067+
</div>
1205012068
</div>
1205112069
<div class="cell border-box-sizing code_cell rendered">
1205212070
<div class="input">
@@ -12071,7 +12089,7 @@ <h2 id="str.replace()"><code>str.replace()</code><a class="anchor-link" href="#s
1207112089

1207212090

1207312091
<div class="output_subarea output_stream output_stdout output_text">
12074-
<pre>Python will be my f?vorite progr?mming l?ngu?ge!
12092+
<pre>Python will be my favorite programming language!
1207512093
</pre>
1207612094
</div>
1207712095
</div>

notebooks/beginner/strings.ipynb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,30 @@
8686
"help(str.replace)"
8787
]
8888
},
89+
{
90+
"cell_type": "markdown",
91+
"metadata": {},
92+
"source": [
93+
"This will not modify `my_string` because replace is not done in-place."
94+
]
95+
},
8996
{
9097
"cell_type": "code",
9198
"execution_count": null,
9299
"metadata": {},
93100
"outputs": [],
94101
"source": [
95-
"my_string = my_string.replace('a', '?')\n",
102+
"my_string.replace('a', '?')\n",
96103
"print(my_string)"
97104
]
98105
},
106+
{
107+
"cell_type": "markdown",
108+
"metadata": {},
109+
"source": [
110+
"You have to store the return value of `replace` instead."
111+
]
112+
},
99113
{
100114
"cell_type": "code",
101115
"execution_count": null,

0 commit comments

Comments
 (0)
0