-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathStrings.html
More file actions
583 lines (525 loc) · 54.1 KB
/
Strings.html
File metadata and controls
583 lines (525 loc) · 54.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Strings — Programming in Python 7.0 documentation</title>
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
<!--[if lt IE 9]>
<script src="../_static/js/html5shiv.min.js"></script>
<![endif]-->
<script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
<script src="../_static/jquery.js"></script>
<script src="../_static/underscore.js"></script>
<script src="../_static/doctools.js"></script>
<script src="../_static/js/theme.js"></script>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="String Formatting Exercise" href="../exercises/string_formatting.html" />
<link rel="prev" title="5. Basic Text Handling" href="../topics/05-text_handling/index.html" />
</head>
<body class="wy-body-for-nav">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search" style="background: #4b2e83" >
<a href="../index.html">
<img src="../_static/UWPCE_logo_full.png" class="logo" alt="Logo"/>
</a>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
<p class="caption" role="heading"><span class="caption-text">Topics in the Program</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../topics/01-setting_up/index.html">1. Setting up your Environment</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/02-basic_python/index.html">2. Basic Python</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/03-recursion_booleans/index.html">3. Booleans and Recursion</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/04-sequences_iteration/index.html">4. Sequences and Iteration</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="../topics/05-text_handling/index.html">5. Basic Text Handling</a><ul class="current">
<li class="toctree-l2 current"><a class="current reference internal" href="#">Strings</a></li>
<li class="toctree-l2"><a class="reference internal" href="../exercises/string_formatting.html">String Formatting Exercise</a></li>
<li class="toctree-l2"><a class="reference internal" href="../exercises/rot13.html">ROT13</a></li>
<li class="toctree-l2"><a class="reference internal" href="../exercises/mailroom/mailroom.html">Mailroom</a></li>
<li class="toctree-l2"><a class="reference internal" href="../exercises/mailroom/mailroom_tutorial.html">Mailroom Tutorial</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../topics/06-exceptions/index.html">6. Exception Handling</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/07-unit_testing/index.html">7. Unit Testing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/08-dicts_sets/index.html">8. Dictionaries and Sets</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/09-files/index.html">9. File Handling</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/10-modules_packages/index.html">10. Modules and Packages</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/11-argument_passing/index.html">11. Advanced Argument Passing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/12-comprehensions/index.html">12. Comprehensions</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/13-intro_oo/index.html">13. Intro to Object Oriented Programing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/14-magic_methods/index.html">14. Properties and Magic Methods</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/15-subclassing/index.html">15. Subclassing and Inheritance</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/16-multiple_inheritance/index.html">16. Multiple Inheritance</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/17-functional_programming/index.html">17. Introduction to Functional Programming</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/18-advanced_testing/index.html">18. Advanced Testing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/99-extras/index.html">19. Extra Topics</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" style="background: #4b2e83" >
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="../index.html">Programming in Python</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content style-external-links">
<div role="navigation" aria-label="Page navigation">
<ul class="wy-breadcrumbs">
<li><a href="../index.html" class="icon icon-home"></a> »</li>
<li><a href="../topics/05-text_handling/index.html"><span class="section-number">5. </span>Basic Text Handling</a> »</li>
<li>Strings</li>
<li class="wy-breadcrumbs-aside">
<a href="../_sources/modules/Strings.rst.txt" rel="nofollow"> View page source</a>
</li>
</ul><div class="rst-breadcrumbs-buttons" role="navigation" aria-label="Sequential page navigation">
<a href="../topics/05-text_handling/index.html" class="btn btn-neutral float-left" title="5. Basic Text Handling" accesskey="p"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
<a href="../exercises/string_formatting.html" class="btn btn-neutral float-right" title="String Formatting Exercise" accesskey="n">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
</div>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<div class="section" id="strings">
<span id="id1"></span><h1>Strings<a class="headerlink" href="#strings" title="Permalink to this headline"></a></h1>
<blockquote>
<div><p>Fun with Strings</p>
</div></blockquote>
<div class="section" id="id2">
<h2>Strings<a class="headerlink" href="#id2" title="Permalink to this headline"></a></h2>
<div class="admonition-joke admonition">
<p class="admonition-title">Joke</p>
<p>A piece of string is new in town, and looking for a drink. He sees a local bar, walks in, sits down, and orders a beer. The bartender looks at him askance, and says: “wait a minute, are you a piece of string?”. “Why yes”, the string replies. The bartender growls back: “We don’t serve your kind in here – you get out!”.</p>
<p>Disappointed, the piece of string leaves and heads down the street to the next bar, but this time, he barely gets to a seat before being yelled at – “we don’t want any string in here – you get out!”</p>
<p>A third bar, and he has a similar encounter. Now he’s getting pretty distraught and confused – “what have they got against string in this town?”, he asks himself. But he’s also tired and thirsty, so he gets an idea.</p>
<p>The piece of string twists himself all up, winding himself around and around. Then he reaches up and fluffs up the top of his head.</p>
<p>Thus prepared, he heads into yet another bar. This time is different. He walks in, sits down, the bartender takes his order – all good. But then just as the bartender is putting his beer down he stops, and looks hard at him: “wait a minute! you’re a piece of string, aren’t you?”.</p>
<p>Full of confidence, the string replies: “Nope, I’m a frayed knot.”</p>
</div>
<p>A “String” is a computerese word for a piece of text – a “string” of characters.</p>
<p>Why “string”?</p>
<p>“String” can be used to mean “a linear sequence – as of characters, words, proteins, etc.”</p>
<p><a class="reference external" href="http://wordnetweb.princeton.edu/perl/webwn?s=string">Definition of string</a></p>
<p>So a string is a sequence of individual letters or characters.</p>
<p>In Python, each character can be a <a class="reference external" href="https://unicode.org/">Unicode</a> character – that is, any character in any language in the world.
Having this built in by default in Python(3) means that you can get very far simply ignoring it – anything you can type on your computer can be used in strings in Python.
If you do need to work with non-English characters, or data encoded in non-utf-8, particularly on Python 2, here are some notes about that: <a class="reference internal" href="Unicode.html#unicode"><span class="std std-ref">Unicode in Python</span></a>.
But for the most part, in Python3 – strings are text, and text is strings, and that’s that. If you know how to type characters (accented, etc.) that are not used in English on your computer, they should “just work”.</p>
<div class="section" id="creating-strings">
<h3>Creating strings:<a class="headerlink" href="#creating-strings" title="Permalink to this headline"></a></h3>
<p>A string literal creates a string type.</p>
<p>(we’ve seen this already…)</p>
<p>A literal can be delineated with single or double quotes, alone or in triples.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="s2">"this is a string"</span>
<span class="s1">'So is this'</span>
<span class="sd">"""and this also"""</span>
<span class="sd">'''and even this.</span>
<span class="sd">Triple quotes preserve newlines</span>
<span class="sd">so this is three lines'''</span>
</pre></div>
</div>
<p>You can also call the string object (<code class="docutils literal notranslate"><span class="pre">str()</span></code>) to “make” a string out of other data types.</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [256]: </span><span class="nb">str</span><span class="p">(</span><span class="mi">34</span><span class="p">)</span>
<span class="gh">Out[256]: </span><span class="go">'34'</span>
</pre></div>
</div>
<p>Strings can also be read from files or other sources of I/O.</p>
</div>
</div>
<div class="section" id="string-methods">
<h2>String Methods<a class="headerlink" href="#string-methods" title="Permalink to this headline"></a></h2>
<p>The python string object is very powerful with lots of methods for common text manipulation. “methods” are functions defined on an object itself (more on that when we get to OO programming). But it means that you have many ways to manipulate text built right into the string objects themselves.</p>
<p>Note that strings are “immutable” – they can not be changed once they have been created. So the string methods all return new strings, rather than change the string in place. Which is kind of handy if you want to string multiple operations together (pun intended …).</p>
<p>Here are just a few of the more common string methods:</p>
<div class="section" id="splitting-and-joining-strings">
<h3>Splitting and Joining Strings<a class="headerlink" href="#splitting-and-joining-strings" title="Permalink to this headline"></a></h3>
<p><code class="docutils literal notranslate"><span class="pre">split</span></code> and <code class="docutils literal notranslate"><span class="pre">join</span></code> can be used to break up a string into pieces, or make one big string out of multiple smaller pieces:</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [167]: </span><span class="n">csv</span> <span class="o">=</span> <span class="s2">"comma,separated,values"</span>
<span class="gp">In [168]: </span><span class="n">csv</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">','</span><span class="p">)</span>
<span class="gh">Out[168]: </span><span class="go">['comma', 'separated', 'values']</span>
<span class="gp">In [169]: </span><span class="n">psv</span> <span class="o">=</span> <span class="s1">'|'</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">csv</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">','</span><span class="p">))</span>
<span class="gp">In [170]: </span><span class="n">psv</span>
<span class="gh">Out[170]: </span><span class="go">'comma|separated|values'</span>
</pre></div>
</div>
<p>It may seem odd at first that <code class="docutils literal notranslate"><span class="pre">.join()</span></code> is a string method, rather than, say, a method on lists. But in fact, it makes a lot of sense. Lists (and tuples, and other sequences) can hold any type of data – and “joining” arbitrary data types doesn’t make any sense. Joining is strictly a string activity.</p>
<p>And you need a string so you can join the parts with something (e.g. a space, or a comma, or …) – therefore, we need a string object in there somewhere anyway.</p>
<p>Lastly, having join() be a string method means that it can join strings in ANY iterable object – not just lists or other built-in sequence types.</p>
<p>So it does make sense. But even if doesn’t make sense to you, that’s the way it is – so remember that you call <code class="docutils literal notranslate"><span class="pre">.join()</span></code> on the string you want to join things with, not on the sequence.</p>
<p>So to be clear: if you have a bunch of strings in a sequence and you want to put them together, you create a string with the character (or characters) you want to join them with, and call join() on that object:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">In</span> <span class="p">[</span><span class="mi">20</span><span class="p">]:</span> <span class="c1"># comma separated:</span>
<span class="n">In</span> <span class="p">[</span><span class="mi">21</span><span class="p">]:</span> <span class="s2">","</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="s2">"these"</span><span class="p">,</span> <span class="s2">"are"</span><span class="p">,</span> <span class="s2">"some"</span><span class="p">,</span> <span class="s2">"strings"</span><span class="p">])</span>
<span class="n">Out</span><span class="p">[</span><span class="mi">21</span><span class="p">]:</span> <span class="s1">'these,are,some,strings'</span>
<span class="n">In</span> <span class="p">[</span><span class="mi">22</span><span class="p">]:</span> <span class="c1"># you can concatenate by joining with the empty string:</span>
<span class="n">In</span> <span class="p">[</span><span class="mi">23</span><span class="p">]:</span> <span class="s2">""</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="s2">"these"</span><span class="p">,</span> <span class="s2">"are"</span><span class="p">,</span> <span class="s2">"some"</span><span class="p">,</span> <span class="s2">"strings"</span><span class="p">])</span>
<span class="n">Out</span><span class="p">[</span><span class="mi">23</span><span class="p">]:</span> <span class="s1">'thesearesomestrings'</span>
</pre></div>
</div>
<p>Maybe not very common, but you can join with a longer string as well:</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [5]: </span><span class="s2">" --#-- "</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="s2">"these"</span><span class="p">,</span> <span class="s2">"are"</span><span class="p">,</span> <span class="s2">"some"</span><span class="p">,</span> <span class="s2">"strings"</span><span class="p">])</span>
<span class="gh">Out[5]: </span><span class="go">'these --#-- are --#-- some --#-- strings'</span>
</pre></div>
</div>
</div>
<div class="section" id="building-up-a-long-string">
<h3>Building up a Long String.<a class="headerlink" href="#building-up-a-long-string" title="Permalink to this headline"></a></h3>
<p>An obvious thing to do is something like:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">msg</span> <span class="o">=</span> <span class="s2">""</span>
<span class="k">for</span> <span class="n">piece</span> <span class="ow">in</span> <span class="n">list_of_stuff</span><span class="p">:</span>
<span class="n">msg</span> <span class="o">+=</span> <span class="n">piece</span>
</pre></div>
</div>
<p>But: strings are immutable – Python needs to create a new string each time you add a piece, which is not very efficient. So it’s better to gather all the pieces together in a list, and then join them together:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">msg</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">piece</span> <span class="ow">in</span> <span class="n">list_of_stuff</span><span class="p">:</span>
<span class="n">msg</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">piece</span><span class="p">)</span>
<span class="s2">" "</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
</pre></div>
</div>
<p>appending to lists is efficient – and so is the <code class="docutils literal notranslate"><span class="pre">join()</span></code> method of strings. In fact the <cite>+=</cite> approach is so inefficient that the <cite>sum()</cite> function explicitly forbids summing strings:</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [2]: </span><span class="nb">sum</span><span class="p">(</span><span class="n">stuff_to_join</span><span class="p">,</span> <span class="s2">""</span><span class="p">)</span>
<span class="gt">---------------------------------------------------------------------------</span>
<span class="ne">TypeError</span><span class="g g-Whitespace"> </span>Traceback (most recent call last)
<span class="o"><</span><span class="n">ipython</span><span class="o">-</span><span class="nb">input</span><span class="o">-</span><span class="mi">2</span><span class="o">-</span><span class="mi">1</span><span class="n">c17e5fa4f59</span><span class="o">></span> <span class="ow">in</span> <span class="o"><</span><span class="n">module</span><span class="o">></span>
<span class="ne">----> </span><span class="mi">1</span> <span class="nb">sum</span><span class="p">(</span><span class="n">stuff_to_join</span><span class="p">,</span> <span class="s2">""</span><span class="p">)</span>
<span class="ne">TypeError</span>: sum() can't sum strings [use ''.join(seq) instead]
</pre></div>
</div>
</div>
<div class="section" id="case-switching">
<h3>Case Switching<a class="headerlink" href="#case-switching" title="Permalink to this headline"></a></h3>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [171]: </span><span class="n">sample</span> <span class="o">=</span> <span class="s1">'A long string of words'</span>
<span class="gp">In [172]: </span><span class="n">sample</span><span class="o">.</span><span class="n">upper</span><span class="p">()</span>
<span class="gh">Out[172]: </span><span class="go">'A LONG STRING OF WORDS'</span>
<span class="gp">In [173]: </span><span class="n">sample</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
<span class="gh">Out[173]: </span><span class="go">'a long string of words'</span>
<span class="gp">In [174]: </span><span class="n">sample</span><span class="o">.</span><span class="n">swapcase</span><span class="p">()</span>
<span class="gh">Out[174]: </span><span class="go">'a LONG STRING OF WORDS'</span>
<span class="gp">In [175]: </span><span class="n">sample</span><span class="o">.</span><span class="n">title</span><span class="p">()</span>
<span class="gh">Out[175]: </span><span class="go">'A Long String Of Words'</span>
</pre></div>
</div>
</div>
<div class="section" id="testing-for-certain-classes-of-characters">
<h3>Testing for certain classes of characters<a class="headerlink" href="#testing-for-certain-classes-of-characters" title="Permalink to this headline"></a></h3>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [181]: </span><span class="n">number</span> <span class="o">=</span> <span class="s2">"12345"</span>
<span class="gp">In [182]: </span><span class="n">number</span><span class="o">.</span><span class="n">isnumeric</span><span class="p">()</span>
<span class="gh">Out[182]: </span><span class="go">True</span>
<span class="gp">In [183]: </span><span class="n">number</span><span class="o">.</span><span class="n">isalnum</span><span class="p">()</span>
<span class="gh">Out[183]: </span><span class="go">True</span>
<span class="gp">In [184]: </span><span class="n">number</span><span class="o">.</span><span class="n">isalpha</span><span class="p">()</span>
<span class="gh">Out[184]: </span><span class="go">False</span>
<span class="gp">In [185]: </span><span class="n">fancy</span> <span class="o">=</span> <span class="s2">"Th!$ $tr!ng h@$ $ymb0l$"</span>
<span class="gp">In [186]: </span><span class="n">fancy</span><span class="o">.</span><span class="n">isalnum</span><span class="p">()</span>
<span class="gh">Out[186]: </span><span class="go">False</span>
</pre></div>
</div>
</div>
<div class="section" id="string-literals">
<h3>String Literals<a class="headerlink" href="#string-literals" title="Permalink to this headline"></a></h3>
<p>Sometimes when you are creating a string, you want to put an non-normal character in there – one that isn’t strictly a letter or symbol, such as newlines, etc.</p>
<p>To do that, python support a set of “escape” sequences – when a character follows a backslash, it gets interpreted as having a particular meaning.</p>
<p>Common Escape Sequences:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>\\ <span class="n">Backslash</span> <span class="p">(</span>\<span class="p">)</span>
\<span class="n">a</span> <span class="n">ASCII</span> <span class="n">Bell</span> <span class="p">(</span><span class="n">BEL</span><span class="p">)</span>
\<span class="n">b</span> <span class="n">ASCII</span> <span class="n">Backspace</span> <span class="p">(</span><span class="n">BS</span><span class="p">)</span>
\<span class="n">n</span> <span class="n">ASCII</span> <span class="n">Linefeed</span> <span class="p">(</span><span class="n">LF</span><span class="p">)</span>
\<span class="n">r</span> <span class="n">ASCII</span> <span class="n">Carriage</span> <span class="n">Return</span> <span class="p">(</span><span class="n">CR</span><span class="p">)</span>
\<span class="n">t</span> <span class="n">ASCII</span> <span class="n">Horizontal</span> <span class="n">Tab</span> <span class="p">(</span><span class="n">TAB</span><span class="p">)</span>
\<span class="n">ooo</span> <span class="n">Character</span> <span class="k">with</span> <span class="n">octal</span> <span class="n">value</span> <span class="n">ooo</span>
\<span class="n">xhh</span> <span class="n">Character</span> <span class="k">with</span> <span class="nb">hex</span> <span class="n">value</span> <span class="n">hh</span>
\<span class="n">uxxxx</span> <span class="n">Character</span> <span class="k">with</span> <span class="n">Unicode</span> <span class="n">code</span> <span class="n">point</span> <span class="n">value</span> <span class="n">xxxx</span>
\<span class="n">N</span><span class="p">{</span><span class="n">char</span><span class="o">-</span><span class="n">name</span><span class="p">}</span> <span class="n">Character</span> <span class="k">with</span> <span class="n">Unicdoe</span> <span class="n">name</span> <span class="n">char_name</span>
</pre></div>
</div>
<p>For example – for tab-separated values:</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [25]: </span><span class="n">s</span> <span class="o">=</span> <span class="s2">"these</span><span class="se">\t</span><span class="s2">are</span><span class="se">\t</span><span class="s2">separated</span><span class="se">\t</span><span class="s2">by</span><span class="se">\t</span><span class="s2">tabs"</span>
<span class="gp">In [12]: </span><span class="nb">print</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
<span class="go">these are separated by tabs</span>
</pre></div>
</div>
<p><a class="reference external" href="https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals">https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals</a></p>
<p><a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#string-methods">https://docs.python.org/3/library/stdtypes.html#string-methods</a></p>
</div>
<div class="section" id="raw-strings">
<h3>Raw Strings<a class="headerlink" href="#raw-strings" title="Permalink to this headline"></a></h3>
<p>There are times when you want a literal backslash in your string: Windows file paths, regular expressions. To make this easy, Python support “raw” strings – string literals where the backslash does not have special meaning:</p>
<p>Add an <code class="docutils literal notranslate"><span class="pre">r</span></code> in front of the string literal:</p>
<p><strong>Escape Sequences Are Ignored</strong></p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [408]: </span><span class="nb">print</span><span class="p">(</span><span class="s2">"this</span><span class="se">\n</span><span class="s2">that"</span><span class="p">)</span>
<span class="go">this</span>
<span class="go">that</span>
<span class="gp">In [409]: </span><span class="nb">print</span><span class="p">(</span><span class="sa">r</span><span class="s2">"this\nthat"</span><span class="p">)</span>
<span class="go">this\nthat</span>
</pre></div>
</div>
<p><strong>Gotcha</strong></p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [415]: </span><span class="sa">r</span><span class="s2">"</span><span class="se">\"</span>
<span class="go">SyntaxError: EOL while scanning string literal</span>
</pre></div>
</div>
<p>Putting a backslash right before the end quote confuses the interpreter!</p>
<p>Raw strings can be very handy for things like regular expressions that need embedded backslashes.</p>
</div>
<div class="section" id="building-long-string-literals">
<h3>Building Long String Literals<a class="headerlink" href="#building-long-string-literals" title="Permalink to this headline"></a></h3>
<p>If you put two string literals next to each other in the code, Python will join them into one when compiling:</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [6]: </span><span class="s2">"this"</span> <span class="s2">"that"</span>
<span class="gh">Out[6]: </span><span class="go">'thisthat'</span>
</pre></div>
</div>
<p>(note: no comma in between!)
This may not look useful, but when combined with the fact that Python joins together lines when inside a parentheses, it can be a nice way to make larger string literals:</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [7]: </span><span class="nb">print</span><span class="p">(</span><span class="s2">"This is the first line</span><span class="se">\n</span><span class="s2">"</span>
<span class="gp"> ...: </span> <span class="s2">"And here is another line</span><span class="se">\n</span><span class="s2">"</span>
<span class="gp"> ...: </span> <span class="s2">"If I don't put in a newline "</span>
<span class="gp"> ...: </span> <span class="s2">"I can get a very long line in, without making the"</span>
<span class="gp"> ...: </span> <span class="s2">"line of code too long."</span><span class="p">)</span>
<span class="go">This is the first line</span>
<span class="go">And here is another line</span>
<span class="go">If I don't put in a newline I can get a very long line in, without making the line of code too long.</span>
</pre></div>
</div>
</div>
<div class="section" id="ordinal-values">
<h3>Ordinal values<a class="headerlink" href="#ordinal-values" title="Permalink to this headline"></a></h3>
<p>Characters in strings are stored as numeric values:</p>
<ul class="simple">
<li><p>“ASCII” values: 1-127</p></li>
<li><p>Unicode “code points” – 1 - 1,114,111 (!!!)</p></li>
</ul>
<p>Unicode supports a LOT of characters – every character in every language known to man – and then some :-). The Unicode code points for the characters in the ASCII character set are the same as ASCII – so handy for us English speakers.</p>
<p>To get the code point value, use <code class="docutils literal notranslate"><span class="pre">ord()</span></code>:</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [109]: </span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="s1">'Chris'</span><span class="p">:</span>
<span class="gp"> .....: </span> <span class="nb">print</span><span class="p">(</span><span class="nb">ord</span><span class="p">(</span><span class="n">i</span><span class="p">),</span> <span class="n">end</span><span class="o">=</span><span class="s1">' '</span><span class="p">)</span>
<span class="go">67 104 114 105 115</span>
</pre></div>
</div>
<p>To get the character from the code point, use <code class="docutils literal notranslate"><span class="pre">chr()</span></code>:</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [110]: </span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="p">(</span><span class="mi">67</span><span class="p">,</span><span class="mi">104</span><span class="p">,</span><span class="mi">114</span><span class="p">,</span><span class="mi">105</span><span class="p">,</span><span class="mi">115</span><span class="p">):</span>
<span class="gp"> .....: </span> <span class="nb">print</span><span class="p">(</span><span class="nb">chr</span><span class="p">(</span><span class="n">i</span><span class="p">),</span> <span class="n">end</span><span class="o">=</span><span class="s1">''</span><span class="p">)</span>
<span class="go">Chris</span>
</pre></div>
</div>
<p>For the English language, stick with ASCII, otherwise use full Unicode: it’s easy with Python3</p>
</div>
<div class="section" id="building-strings-from-data">
<h3>Building Strings from Data<a class="headerlink" href="#building-strings-from-data" title="Permalink to this headline"></a></h3>
<p>We often have some data in Python variables – maybe strings, maybe numbers – and we often want to combine that data with text to make a custom message of some sort.</p>
<p>You could, but please don’t(!), do this:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="s1">'Hello '</span> <span class="o">+</span> <span class="n">name</span> <span class="o">+</span> <span class="s1">'!'</span>
</pre></div>
</div>
<p>(I know – we did that in the grid_printing exercise)</p>
<p>Why not? It’s slow and not very flexible. Python provides a few ways to “format” text, so you can do this instead:</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [11]: </span><span class="s1">'Hello </span><span class="si">{}</span><span class="s1">!'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
<span class="gh">Out[11]: </span><span class="go">'Hello Chris!'</span>
</pre></div>
</div>
<p>It’s much faster and safer, and easier to modify as code gets complicated.</p>
<p><a class="reference external" href="https://docs.python.org/3/library/string.html#string-formatting">https://docs.python.org/3/library/string.html#string-formatting</a></p>
</div>
<div class="section" id="old-and-new-string-formatting">
<h3>Old and New string formatting<a class="headerlink" href="#old-and-new-string-formatting" title="Permalink to this headline"></a></h3>
<p>Back in early Python days, there was the string formatting operator: <code class="docutils literal notranslate"><span class="pre">%</span></code></p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="s2">"a string: </span><span class="si">%s</span><span class="s2"> and a number: </span><span class="si">%i</span><span class="s2"> "</span><span class="o">%</span><span class="p">(</span><span class="s2">"text"</span><span class="p">,</span> <span class="mi">45</span><span class="p">)</span>
</pre></div>
</div>
<p>This is very similar to C-style string formatting (<cite>sprintf</cite>).</p>
<p>It’s still around, and handy — but …</p>
<p>The “new” <code class="docutils literal notranslate"><span class="pre">format()</span></code> method is more powerful and flexible, so we’ll focus on that in this class. And there is now the newer “f-strings” (see below) which provide a lot of that “quick and dirty” convenience, while using the same formatting codes as <code class="docutils literal notranslate"><span class="pre">.format()</span></code>. So there really isn’t a reason to use the “old style” anymore.</p>
</div>
<div class="section" id="string-formatting">
<h3>String Formatting<a class="headerlink" href="#string-formatting" title="Permalink to this headline"></a></h3>
<p>The string <code class="docutils literal notranslate"><span class="pre">.format()</span></code> method:</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [62]: </span><span class="s2">"A decimal integer is: </span><span class="si">{:d}</span><span class="s2">"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="mi">34</span><span class="p">)</span>
<span class="gh">Out[62]: </span><span class="go">'A decimal integer is: 34'</span>
<span class="gp">In [63]: </span><span class="s2">"a floating point is: </span><span class="si">{:f}</span><span class="s2">"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="mf">34.5</span><span class="p">)</span>
<span class="gh">Out[63]: </span><span class="go">'a floating point is: 34.500000'</span>
<span class="gp">In [64]: </span><span class="s2">"a string is the default: </span><span class="si">{}</span><span class="s2">"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="s2">"anything"</span><span class="p">)</span>
<span class="gh">Out[64]: </span><span class="go">'a string is the default: anything'</span>
</pre></div>
</div>
</div>
<div class="section" id="multiple-placeholders">
<h3>Multiple placeholders:<a class="headerlink" href="#multiple-placeholders" title="Permalink to this headline"></a></h3>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [65]: </span><span class="s2">"the number is </span><span class="si">{}</span><span class="s2"> is </span><span class="si">{}</span><span class="s2">"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="s1">'five'</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span>
<span class="gh">Out[65]: </span><span class="go">'the number is five is 5'</span>
<span class="gp">In [66]: </span><span class="s2">"the first 3 numbers are </span><span class="si">{}</span><span class="s2">, </span><span class="si">{}</span><span class="s2">, </span><span class="si">{}</span><span class="s2">"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">)</span>
<span class="gh">Out[66]: </span><span class="go">'the first 3 numbers are 1, 2, 3'</span>
</pre></div>
</div>
<p>The counts must agree:</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [67]: </span><span class="s2">"string with </span><span class="si">{}</span><span class="s2"> formatting </span><span class="si">{}</span><span class="s2">"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="gt">---------------------------------------------------------------------------</span>
<span class="ne">IndexError</span><span class="g g-Whitespace"> </span>Traceback (most recent call last)
<span class="nn"><ipython-input-67-a079bc472aca></span> in <span class="ni"><module></span><span class="nt">()</span>
<span class="ne">----> </span><span class="mi">1</span> <span class="s2">"string with </span><span class="si">{}</span><span class="s2"> formatting </span><span class="si">{}</span><span class="s2">"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="ne">IndexError</span>: tuple index out of range
</pre></div>
</div>
</div>
<div class="section" id="named-placeholders">
<h3>Named Placeholders:<a class="headerlink" href="#named-placeholders" title="Permalink to this headline"></a></h3>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [69]: </span><span class="s2">"Hello, </span><span class="si">{name}</span><span class="s2">, whadaya know?"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s2">"Joe"</span><span class="p">)</span>
<span class="gh">Out[69]: </span><span class="go">'Hello, Joe, whadaya know?'</span>
</pre></div>
</div>
<p>You can use values more than once, and skip values:</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [73]: </span><span class="s2">"Hi, </span><span class="si">{name}</span><span class="s2">. Howzit, </span><span class="si">{name}</span><span class="s2">?"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">'Bob'</span><span class="p">)</span>
<span class="gh">Out[73]: </span><span class="go">'Hi, Bob. Howzit, Bob?'</span>
</pre></div>
</div>
<p>The format operator works with string variables, too:</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [80]: </span><span class="n">s</span> <span class="o">=</span> <span class="s2">"</span><span class="si">{:d}</span><span class="s2"> / </span><span class="si">{:d}</span><span class="s2"> = </span><span class="si">{:f}</span><span class="s2">"</span>
<span class="gp">In [81]: </span><span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">3</span>
<span class="gp">In [82]: </span><span class="n">s</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">a</span><span class="o">/</span><span class="n">b</span><span class="p">)</span>
<span class="gh">Out[82]: </span><span class="go">'12 / 3 = 4.000000'</span>
</pre></div>
</div>
<p>So you can save a format string, or even built it up dynamically, and then use it in multiple places in the code.</p>
</div>
<div class="section" id="complex-formatting">
<h3>Complex Formatting<a class="headerlink" href="#complex-formatting" title="Permalink to this headline"></a></h3>
<p>There is a complete syntax for specifying all sorts of options.</p>
<p>It’s well worth your while to spend some time getting to know this
<a class="reference external" href="https://docs.python.org/3/library/string.html#format-specification-mini-language">formatting language</a>. You can accomplish a great deal just with this.</p>
<p>Here is a nice tutorial:</p>
<p><a class="reference external" href="https://pyformat.info/">https://pyformat.info/</a></p>
<p>And a nice formatting cookbook:</p>
<p><a class="reference external" href="https://mkaz.blog/code/python-string-format-cookbook/">https://mkaz.blog/code/python-string-format-cookbook/</a></p>
</div>
</div>
<div class="section" id="literal-string-interpolation">
<h2>Literal String Interpolation<a class="headerlink" href="#literal-string-interpolation" title="Permalink to this headline"></a></h2>
<p>In Python 3.6, yet another string formatting method was introduced.</p>
<p>Known at “f-strings”, or more formally, “Literal String Interpolation”, they provide a concise, readable way to include the value of Python expressions inside strings. In particular, they make it easy to include names in the current namespace without having to type them multiple times.</p>
<p>For example:</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [24]: </span><span class="n">first</span> <span class="o">=</span> <span class="s2">"Chris"</span>
<span class="gp">In [25]: </span><span class="n">last</span> <span class="o">=</span> <span class="s2">"Barker"</span>
<span class="gp">In [26]: </span><span class="sa">f</span><span class="s2">"My name is </span><span class="si">{</span><span class="n">first</span><span class="si">}</span><span class="s2"> </span><span class="si">{</span><span class="n">last</span><span class="si">}</span><span class="s2">"</span>
<span class="gh">Out[26]: </span><span class="go">'My name is Chris Barker'</span>
</pre></div>
</div>
<p>Note that they are called “f-strings” because they are created by putting and “f” before the string – “f” is for format.</p>
<p>All the other ways to do this required a lot more typing:</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [28]: </span><span class="s2">"My name is </span><span class="si">{first}</span><span class="s2"> </span><span class="si">{last}</span><span class="s2">"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">first</span><span class="o">=</span><span class="n">first</span><span class="p">,</span> <span class="n">last</span><span class="o">=</span><span class="n">last</span><span class="p">)</span>
<span class="gh">Out[28]: </span><span class="go">'My name is Chris Barker'</span>
<span class="gp">In [29]: </span><span class="s2">"My name is </span><span class="si">{}</span><span class="s2"> </span><span class="si">{}</span><span class="s2">"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">first</span><span class="p">,</span> <span class="n">last</span><span class="p">)</span>
<span class="gh">Out[29]: </span><span class="go">'My name is Chris Barker'</span>
<span class="gp">In [30]: </span><span class="s2">"My name is </span><span class="si">%s</span><span class="s2"> </span><span class="si">%s</span><span class="s2">"</span> <span class="o">%</span> <span class="p">(</span><span class="n">first</span><span class="p">,</span> <span class="n">last</span><span class="p">)</span>
<span class="gh">Out[30]: </span><span class="go">'My name is Chris Barker'</span>
</pre></div>
</div>
<p>Even more than the typing, it required keeping the string and the input data in sync when you changed things: maybe adding or removing an interpolated item.</p>
<div class="section" id="f-string-basics">
<h3>f-string basics<a class="headerlink" href="#f-string-basics" title="Permalink to this headline"></a></h3>
<p>f-strings are actually pretty simple concept:</p>
<p>You can interpolate the stringifcation of any expression into a string at run time. Variables are all evaluated at the current scope.</p>
<p>The expression is put inside curly brackets: {}, the same as for the <code class="docutils literal notranslate"><span class="pre">.format</span></code> method.</p>
<p>So what does that all mean?</p>
<p>For this most simple example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="sa">f</span><span class="s2">"some text: </span><span class="si">{</span><span class="n">expression</span><span class="si">}</span><span class="s2">"</span>
</pre></div>
</div>
<p><cite>expression</cite> is any valid python expression (remember that an expression is a combination of values and operators and names that produces a value).</p>
<p>The expression is evaluated, and then, if it is not a string, it is converted to one, so it’s really:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="sa">f</span><span class="s2">"some text: </span><span class="si">{</span><span class="nb">str</span><span class="p">(</span><span class="n">expression</span><span class="p">)</span><span class="si">}</span><span class="s2">"</span>
</pre></div>
</div>
<p>Let’s see how this works in practice:</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [32]: </span><span class="c1"># define a couple of names:</span>
<span class="gp">In [33]: </span><span class="n">x</span> <span class="o">=</span> <span class="mi">5</span>
<span class="gp">In [34]: </span><span class="n">y</span> <span class="o">=</span> <span class="mi">12</span>
<span class="gp">In [35]: </span><span class="n">name</span> <span class="o">=</span> <span class="s2">"fred"</span>
<span class="gp">In [36]: </span><span class="c1"># a simple string:</span>
<span class="gp">In [37]: </span><span class="sa">f</span><span class="s2">"some text: </span><span class="si">{</span><span class="n">name</span><span class="si">}</span><span class="s2">"</span>
<span class="gh">Out[37]: </span><span class="go">'some text: fred'</span>
<span class="gp">In [38]: </span><span class="c1"># if it's not a string, it will be turned into one:</span>
<span class="gp">In [39]: </span><span class="sa">f</span><span class="s2">"some text: </span><span class="si">{</span><span class="n">x</span><span class="si">}</span><span class="s2">"</span>
<span class="gh">Out[39]: </span><span class="go">'some text: 5'</span>
<span class="gp">In [40]: </span><span class="c1"># but you can do a more complex expression as well:</span>
<span class="gp">In [41]: </span><span class="sa">f</span><span class="s2">"some text: </span><span class="si">{</span><span class="n">x</span> <span class="o">+</span> <span class="n">y</span><span class="si">}</span><span class="s2">"</span>
<span class="gh">Out[41]: </span><span class="go">'some text: 17'</span>
<span class="gp">In [42]: </span><span class="c1"># and call methods:</span>
<span class="gp">In [43]: </span><span class="sa">f</span><span class="s2">"some text: </span><span class="si">{</span><span class="n">name</span><span class="o">.</span><span class="n">capitalize</span><span class="p">()</span><span class="si">}</span><span class="s2">"</span>
<span class="gh">Out[43]: </span><span class="go">'some text: Fred'</span>
<span class="gp">In [45]: </span><span class="c1"># even boolean expressions:</span>
<span class="gp">In [46]: </span><span class="sa">f</span><span class="s2">"some text: </span><span class="si">{</span><span class="n">name</span> <span class="k">if</span> <span class="n">x</span> <span class="o"><</span> <span class="mi">5</span> <span class="k">else</span> <span class="n">name2</span><span class="si">}</span><span class="s2">"</span>
<span class="gh">Out[46]: </span><span class="go">'some text: bob'</span>
</pre></div>
</div>
<p>You can put ANY expression in there – no matter how complex. But do be careful, if it’s too complex, it will just make the code harder to read!</p>
<p>And it has to be an expression, not a statement – so you can’t put a for loop or anything like that in there.</p>
<p>You can see how this can be a very powerful and quick way to get things done.</p>
</div>
<div class="section" id="formatting-codes-with-f-strings">
<h3>Formatting codes with f-strings<a class="headerlink" href="#formatting-codes-with-f-strings" title="Permalink to this headline"></a></h3>
<p>We’ve seen that f-strings will automatically “stringify” the results of the expression used.
And that’s often what you want.
But if you do want to control how that is done, you can use all the same formatting codes used with the <code class="docutils literal notranslate"><span class="pre">.format()</span></code> method:</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [15]: </span><span class="sa">f</span><span class="s2">"One third with 4 digits is: </span><span class="si">{</span><span class="mi">1</span><span class="o">/</span><span class="mi">3</span><span class="si">:</span><span class="s2">.3f</span><span class="si">}</span><span class="s2">"</span>
<span class="gh">Out[15]: </span><span class="go">'One third with 4 digits is: 0.333'</span>
</pre></div>
</div>
<p>you put the format specifier after a colon.</p>
</div>
<div class="section" id="f-string-use">
<h3>f-string Use<a class="headerlink" href="#f-string-use" title="Permalink to this headline"></a></h3>
<p>f-strings are a fairly new Python feature. They will cause a syntax error in any Python version older than 3.6 – 3.6 was first released on December 23, 2016 – only a couple years from this writing.</p>
<p>So there is still not much out there in the wild, and it it still rare in production code.</p>
<p>They are not currently used in many of the examples in this course.</p>
<p>Nevertheless, they are a nifty feature that could be very useful, so feel free to use them where it makes you code cleaner and clearer.</p>
</div>
<div class="section" id="more-on-f-strings">
<h3>More on f-strings<a class="headerlink" href="#more-on-f-strings" title="Permalink to this headline"></a></h3>
<p>To read all about the justification and syntax, read PEP 498:</p>
<p><a class="reference external" href="https://www.python.org/dev/peps/pep-0498/">https://www.python.org/dev/peps/pep-0498/</a></p>
</div>
<div class="section" id="other-resources-for-f-strings">
<h3>Other resources for f-strings<a class="headerlink" href="#other-resources-for-f-strings" title="Permalink to this headline"></a></h3>
<p>f-strings have been around a while now, so there are a number of good introductions out there:</p>
<p>A short introduction:</p>
<p><a class="reference external" href="https://cito.github.io/blog/f-strings/">https://cito.github.io/blog/f-strings/</a></p>
<p>Another intro:</p>
<p><a class="reference external" href="https://www.pydanny.com/python-f-strings-are-fun.html">https://www.pydanny.com/python-f-strings-are-fun.html</a></p>
<p><a class="reference external" href="http://zetcode.com/python/fstring/">http://zetcode.com/python/fstring/</a></p>
<p>One that gets into the technical details (bytecode! – for the real geeks):</p>
<p><a class="reference external" href="https://hackernoon.com/a-closer-look-at-how-python-f-strings-work-f197736b3bdb">https://hackernoon.com/a-closer-look-at-how-python-f-strings-work-f197736b3bdb</a></p>
</div>
</div>
</div>
</div>
</div>
<footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
<a href="../topics/05-text_handling/index.html" class="btn btn-neutral float-left" title="5. Basic Text Handling" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
<a href="../exercises/string_formatting.html" class="btn btn-neutral float-right" title="String Formatting Exercise" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
</div>
<hr/>
<div role="contentinfo">
<p>© Copyright 2020, University of Washington, Natasha Aleksandrova, Christopher Barker, Brian Dorsey, Cris Ewing, Christy Heaton, Jon Jacky, Maria McKinley, Andy Miles, Rick Riehle, Joseph Schilz, Joseph Sheedy, Hosung Song. Creative Commons Attribution-ShareAlike 4.0 license.</p>
</div>
Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
<a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script>
jQuery(function () {
SphinxRtdTheme.Navigation.enable(true);
});
</script>
</body>
</html>