156
156
div.columns{display: flex; gap: min(4vw, 1.5em);}
157
157
div.column{flex: auto; overflow-x: auto;}
158
158
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
159
- ul.task-list{list-style: none;}
159
+ /* The extra [class] is a hack that increases specificity enough to
160
+ override a similar rule in reveal.js */
161
+ ul.task-list[class]{list-style: none;}
160
162
ul.task-list li input[type="checkbox"] {
163
+ font-size: inherit;
161
164
width: 0.8em;
162
165
margin: 0 0.8em 0.2em -1.6em;
163
166
vertical-align: middle;
@@ -506,55 +509,36 @@ class="uri">https://docs.python.org/library/stdtypes.html#string-methods</a></p>
506
509
<p><a
507
510
href="https://docs.python.org/library/stdtypes.html#string-methods"
508
511
class="uri">https://docs.python.org/library/stdtypes.html#string-methods</a>.</p>
509
- <h2 id="format-operator">Format operator</h2>
510
- <p> </p>
511
- <p>The <em>format operator</em>, <code>%</code> allows us to construct
512
- strings, replacing parts of the strings with the data stored in
513
- variables. When applied to integers, <code>%</code> is the modulus
514
- operator. But when the first operand is a string, <code>%</code> is the
515
- format operator.</p>
516
- <p></p>
517
- <p>The first operand is the <em>format string</em>, which contains one
518
- or more <em>format sequences</em> that specify how the second operand is
519
- formatted. The result is a string.</p>
512
+ <h2 id="formatted-string-literals">Formatted String Literals</h2>
520
513
<p></p>
521
- <p>For example, the format sequence <code>%d</code> means that the
522
- second operand should be formatted as an integer (“d” stands for
523
- “decimal”):</p>
514
+ <p>A formatted string literal (often referred to simply as an f-string)
515
+ allows Python expressions to be used within string literals. This is
516
+ accomplished by prepending an <code>f</code> to the string literal and
517
+ enclosing expressions in curly braces <code>{}</code>.</p>
518
+ <p>For example, wrapping a variable name in curly braces inside an
519
+ f-string will cause it to be replaced by its value:</p>
524
520
<pre class="python"><code>>>> camels = 42
525
- >>> '%d ' % camels
521
+ >>> f '{camels} '
526
522
'42'</code></pre>
527
523
<p>The result is the string ‘42’, which is not to be confused with the
528
524
integer value 42.</p>
529
- <p>A format sequence can appear anywhere in the string, so you can embed
530
- a value in a sentence:</p>
525
+ <p>An expression can appear anywhere in the string, so you can embed a
526
+ value in a sentence:</p>
531
527
<pre class="python"><code>>>> camels = 42
532
- >>> 'I have spotted %d camels.' % camels
528
+ >>> f 'I have spotted {camels} camels.'
533
529
'I have spotted 42 camels.'</code></pre>
534
- <p>If there is more than one format sequence in the string, the second
535
- argument has to be a tuple<a href="#fn1" class="footnote-ref"
536
- id="fnref1" role="doc-noteref"><sup>1</sup></a>. Each format sequence is
537
- matched with an element of the tuple, in order.</p>
538
- <p>The following example uses <code>%d</code> to format an integer,
539
- <code>%g</code> to format a floating-point number (don’t ask why), and
540
- <code>%s</code> to format a string:</p>
541
- <pre class="python"><code>>>> 'In %d years I have spotted %g %s.' % (3, 0.1, 'camels')
530
+ <p>Several expressions can be included within a single string literal in
531
+ order to create more complex strings.</p>
532
+ <pre class="python"><code>>>> years = 3
533
+ >>> count = .1
534
+ >>> species = 'camels'
535
+ >>> f'In {years} years I have spotted {count} {species}.'
542
536
'In 3 years I have spotted 0.1 camels.'</code></pre>
543
- <p>The number of elements in the tuple must match the number of format
544
- sequences in the string. The types of the elements also must match the
545
- format sequences:</p>
546
- <p> </p>
547
- <pre class="python"><code>>>> '%d %d %d' % (1, 2)
548
- TypeError: not enough arguments for format string
549
- >>> '%d' % 'dollars'
550
- TypeError: %d format: a number is required, not str</code></pre>
551
- <p>In the first example, there aren’t enough elements; in the second,
552
- the element is the wrong type.</p>
553
- <p>The format operator is powerful, but it can be difficult to use. You
554
- can read more about it at</p>
537
+ <p>Formatted string literals are powerful, and they can do even more
538
+ than is covered here. You can read more about them at</p>
555
539
<p><a
556
- href="https://docs.python.org/library/stdtypes .html#printf-style- string-formatting "
557
- class="uri">https://docs.python.org/library/stdtypes .html#printf-style- string-formatting </a>.</p>
540
+ href="https://docs.python.org/3/tutorial/inputoutput .html#formatted- string-literals "
541
+ class="uri">https://docs.python.org/3/tutorial/inputoutput .html#formatted- string-literals </a>.</p>
558
542
<h2 id="debugging">Debugging</h2>
559
543
<p></p>
560
544
<p>A skill that you should cultivate as you program is always asking
@@ -684,7 +668,7 @@ class="uri">https://docs.python.org/library/stdtypes.html#printf-style-string-fo
684
668
<p> </p>
685
669
<p><strong>Exercise 6: Read the documentation of the string methods at
686
670
<a href="https://docs.python.org/library/stdtypes.html#string-methods"
687
- class="uri">https://docs.python.org/library/stdtypes.html#string-methods</a>
671
+ class="uri">https://docs.python.org/library/stdtypes.html#string-methods</a>.
688
672
You might want to experiment with some of them to make sure you
689
673
understand how they work. <code>strip</code> and <code>replace</code>
690
674
are particularly useful.</strong></p>
@@ -693,15 +677,6 @@ class="uri">https://docs.python.org/library/stdtypes.html#string-methods</a>
693
677
indicate optional arguments. So <code>sub</code> is required, but
694
678
<code>start</code> is optional, and if you include <code>start</code>,
695
679
then <code>end</code> is optional.</strong></p>
696
- <aside id="footnotes" class="footnotes footnotes-end-of-document"
697
- role="doc-endnotes">
698
- <hr />
699
- <ol>
700
- <li id="fn1"><p>A tuple is a sequence of comma-separated values inside a
701
- pair of parenthesis. We will cover tuples in Chapter 10<a href="#fnref1"
702
- class="footnote-back" role="doc-backlink">↩︎</a></p></li>
703
- </ol>
704
- </aside>
705
680
</body>
706
681
</html>
707
682
<?php if ( file_exists ("../bookfoot.php " ) ) {
0 commit comments