-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathlexical_analysis.html
More file actions
1786 lines (1735 loc) · 153 KB
/
Copy pathlexical_analysis.html
File metadata and controls
1786 lines (1735 loc) · 153 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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="fa" data-content_root="../">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
<meta property="og:title" content="2. Lexical analysis" />
<meta property="og:type" content="website" />
<meta property="og:url" content="https://docs.python.org/3/reference/lexical_analysis.html" />
<meta property="og:site_name" content="Python documentation" />
<meta property="og:description" content="A Python program is read by a parser. Input to the parser is a stream of tokens, generated by the lexical analyzer(also known as the tokenizer). This chapter describes how the lexical analyzer prod..." />
<meta property="og:image" content="_static/og-image.png" />
<meta property="og:image:alt" content="Python documentation" />
<meta name="description" content="A Python program is read by a parser. Input to the parser is a stream of tokens, generated by the lexical analyzer(also known as the tokenizer). This chapter describes how the lexical analyzer prod..." />
<meta name="theme-color" content="#3776ab">
<meta property="og:image:width" content="200">
<meta property="og:image:height" content="200">
<title>2. Lexical analysis — مستندات Python3.14.6</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=b86133f3" />
<link rel="stylesheet" type="text/css" href="../_static/classic.css?v=234b1a7c" />
<link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=4365c8fe" />
<link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=0fc419ee" />
<script src="../_static/documentation_options.js?v=e254dbbb"></script>
<script src="../_static/doctools.js?v=9bcbadda"></script>
<script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
<script src="../_static/translations.js?v=5df48d09"></script>
<script src="../_static/sidebar.js"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="جستجو در مستندات Python3.14.6"
href="../_static/opensearch.xml"/>
<link rel="author" title="درباره این مستندات" href="../about.html" />
<link rel="index" title="فهرست" href="../genindex.html" />
<link rel="search" title="جستجو" href="../search.html" />
<link rel="copyright" title="حق چاپ" href="../copyright.html" />
<link rel="next" title="3. Data model" href="datamodel.html" />
<link rel="prev" title="1. Introduction" href="introduction.html" />
<link rel="canonical" href="https://docs.python.org/3/reference/lexical_analysis.html">
<style>
@media only screen {
table.full-width-table {
width: 100%;
}
}
</style>
<link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
<link rel="shortcut icon" type="image/png" href="../_static/py.svg">
<script type="text/javascript" src="../_static/copybutton.js"></script>
<script type="text/javascript" src="../_static/menu.js"></script>
<script type="text/javascript" src="../_static/search-focus.js"></script>
<script type="text/javascript" src="../_static/themetoggle.js"></script>
<script type="text/javascript" src="../_static/rtd_switcher.js"></script>
<meta name="readthedocs-addons-api-version" content="1">
</head>
<body>
<div class="mobile-nav">
<input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu">
<nav class="nav-content" role="navigation">
<label for="menuToggler" class="toggler__label">
<span></span>
</label>
<span class="nav-items-wrapper">
<a href="https://www.python.org/" class="nav-logo">
<img src="../_static/py.svg" alt="Python logo">
</a>
<span class="version_switcher_placeholder"></span>
<form role="search" class="search" action="../search.html" method="get">
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
<path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
</svg>
<input placeholder="جستجو سریع" aria-label="جستجو سریع" type="search" name="q">
<input type="submit" value="برو">
</form>
</span>
</nav>
<div class="menu-wrapper">
<nav class="menu" role="navigation" aria-label="main navigation">
<div class="language_switcher_placeholder"></div>
<label class="theme-selector-label">
Theme
<select class="theme-selector" oninput="activateTheme(this.value)">
<option value="auto" selected>Auto</option>
<option value="light">Light</option>
<option value="dark">Dark</option>
</select>
</label>
<div>
<h3><a href="../contents.html">فهرست عناوین</a></h3>
<ul>
<li><a class="reference internal" href="#">2. Lexical analysis</a><ul>
<li><a class="reference internal" href="#line-structure">2.1. Line structure</a><ul>
<li><a class="reference internal" href="#logical-lines">2.1.1. Logical lines</a></li>
<li><a class="reference internal" href="#physical-lines">2.1.2. Physical lines</a></li>
<li><a class="reference internal" href="#comments">2.1.3. Comments</a></li>
<li><a class="reference internal" href="#encoding-declarations">2.1.4. Encoding declarations</a></li>
<li><a class="reference internal" href="#explicit-line-joining">2.1.5. Explicit line joining</a></li>
<li><a class="reference internal" href="#implicit-line-joining">2.1.6. Implicit line joining</a></li>
<li><a class="reference internal" href="#blank-lines">2.1.7. Blank lines</a></li>
<li><a class="reference internal" href="#indentation">2.1.8. Indentation</a></li>
<li><a class="reference internal" href="#whitespace-between-tokens">2.1.9. Whitespace between tokens</a></li>
<li><a class="reference internal" href="#end-marker">2.1.10. End marker</a></li>
</ul>
</li>
<li><a class="reference internal" href="#other-tokens">2.2. Other tokens</a></li>
<li><a class="reference internal" href="#names-identifiers-and-keywords">2.3. Names (identifiers and keywords)</a><ul>
<li><a class="reference internal" href="#keywords">2.3.1. Keywords</a></li>
<li><a class="reference internal" href="#soft-keywords">2.3.2. Soft Keywords</a></li>
<li><a class="reference internal" href="#reserved-classes-of-identifiers">2.3.3. Reserved classes of identifiers</a></li>
<li><a class="reference internal" href="#non-ascii-characters-in-names">2.3.4. Non-ASCII characters in names</a></li>
</ul>
</li>
<li><a class="reference internal" href="#literals">2.4. Literals</a></li>
<li><a class="reference internal" href="#string-and-bytes-literals">2.5. String and Bytes literals</a><ul>
<li><a class="reference internal" href="#triple-quoted-strings">2.5.1. Triple-quoted strings</a></li>
<li><a class="reference internal" href="#string-prefixes">2.5.2. String prefixes</a></li>
<li><a class="reference internal" href="#formal-grammar">2.5.3. Formal grammar</a></li>
<li><a class="reference internal" href="#escape-sequences">2.5.4. Escape sequences</a><ul>
<li><a class="reference internal" href="#ignored-end-of-line">2.5.4.1. Ignored end of line</a></li>
<li><a class="reference internal" href="#escaped-characters">2.5.4.2. Escaped characters</a></li>
<li><a class="reference internal" href="#octal-character">2.5.4.3. Octal character</a></li>
<li><a class="reference internal" href="#hexadecimal-character">2.5.4.4. Hexadecimal character</a></li>
<li><a class="reference internal" href="#named-unicode-character">2.5.4.5. Named Unicode character</a></li>
<li><a class="reference internal" href="#hexadecimal-unicode-characters">2.5.4.6. Hexadecimal Unicode characters</a></li>
<li><a class="reference internal" href="#unrecognized-escape-sequences">2.5.4.7. Unrecognized escape sequences</a></li>
</ul>
</li>
<li><a class="reference internal" href="#bytes-literals">2.5.5. Bytes literals</a></li>
<li><a class="reference internal" href="#raw-string-literals">2.5.6. Raw string literals</a></li>
<li><a class="reference internal" href="#formatted-string-literals">2.5.7. f-strings</a></li>
<li><a class="reference internal" href="#template-string-literals">2.5.8. t-strings</a></li>
<li><a class="reference internal" href="#formal-grammar-for-f-strings">2.5.9. Formal grammar for f-strings</a></li>
</ul>
</li>
<li><a class="reference internal" href="#numeric-literals">2.6. Numeric literals</a><ul>
<li><a class="reference internal" href="#integer-literals">2.6.1. Integer literals</a></li>
<li><a class="reference internal" href="#floating-point-literals">2.6.2. Floating-point literals</a></li>
<li><a class="reference internal" href="#imaginary-literals">2.6.3. Imaginary literals</a></li>
</ul>
</li>
<li><a class="reference internal" href="#operators-and-delimiters">2.7. Operators and delimiters</a></li>
</ul>
</li>
</ul>
</div>
<div>
<h4>موضوع قبلی</h4>
<p class="topless"><a href="introduction.html"
title="فصل قبلی"><span class="section-number">1. </span>Introduction</a></p>
</div>
<div>
<h4>موضوع بعدی</h4>
<p class="topless"><a href="datamodel.html"
title="فصل بعدی"><span class="section-number">3. </span>Data model</a></p>
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
const title = document.querySelector('meta[property="og:title"]').content;
const elements = document.querySelectorAll('.improvepage');
const pageurl = window.location.href.split('?')[0];
elements.forEach(element => {
const url = new URL(element.href.split('?')[0].replace("-nojs", ""));
url.searchParams.set('pagetitle', title);
url.searchParams.set('pageurl', pageurl);
url.searchParams.set('pagesource', "reference/lexical_analysis.rst");
element.href = url.toString();
});
});
</script>
<div role="note" aria-label="source link">
<h3>این صفحه</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">گزارش یک اشکال</a></li>
<li><a class="improvepage" href="../improve-page-nojs.html">بهبود این صفحه</a></li>
<li>
<a href="https://github.com/python/cpython/blob/main/Doc/reference/lexical_analysis.rst?plain=1"
rel="nofollow">نمایش منبع
</a>
</li>
<li>
<a href="https://github.com/python/python-docs-fa/blob/3.14/reference/lexical_analysis.po?plain=1"
rel="nofollow">نمایش منبع ترجمه</a>
</li>
</ul>
</div>
</nav>
</div>
</div>
<div class="related" role="navigation" aria-label="Related">
<h3>ناوبری</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="فهرست کلی"
accesskey="I">فهرست</a></li>
<li class="right" >
<a href="../py-modindex.html" title="نمایه ی ماژول های پایتون"
>ماژول ها</a> |</li>
<li class="right" >
<a href="datamodel.html" title="3. Data model"
accesskey="N">بعدی</a> |</li>
<li class="right" >
<a href="introduction.html" title="1. Introduction"
accesskey="P">قبلی</a> |</li>
<li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>
<li><a href="https://www.python.org/">Python</a> »</li>
<li class="switchers">
<div class="language_switcher_placeholder"></div>
<div class="version_switcher_placeholder"></div>
</li>
<li>
</li>
<li id="cpython-language-and-version">
<a href="../index.html">3.14.6 Documentation</a> »
</li>
<li class="nav-item nav-item-1"><a href="index.html" accesskey="U">The Python Language Reference</a> »</li>
<li class="nav-item nav-item-this"><a href=""><span class="section-number">2. </span>Lexical analysis</a></li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="جستجو سریع" aria-label="جستجو سریع" type="search" name="q" id="search-box">
<input type="submit" value="برو">
</form>
</div>
|
</li>
<li class="right">
<label class="theme-selector-label">
Theme
<select class="theme-selector" oninput="activateTheme(this.value)">
<option value="auto" selected>Auto</option>
<option value="light">Light</option>
<option value="dark">Dark</option>
</select>
</label> |</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<section id="lexical-analysis">
<span id="lexical"></span><h1><span class="section-number">2. </span>Lexical analysis<a class="headerlink" href="#lexical-analysis" title="Link to this heading">¶</a></h1>
<p id="index-0">A Python program is read by a <em>parser</em>. Input to the parser is a stream of
<a class="reference internal" href="../glossary.html#term-token"><span class="xref std std-term">tokens</span></a>, generated by the <em>lexical analyzer</em> (also known as
the <em>tokenizer</em>).
This chapter describes how the lexical analyzer produces these tokens.</p>
<p>The lexical analyzer determines the program text's <a class="reference internal" href="#encodings"><span class="std std-ref">encoding</span></a>
(UTF-8 by default), and decodes the text into
<a class="reference internal" href="#lexical-source-character"><span class="std std-ref">source characters</span></a>.
If the text cannot be decoded, a <a class="reference internal" href="../library/exceptions.html#SyntaxError" title="SyntaxError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SyntaxError</span></code></a> is raised.</p>
<p>Next, the lexical analyzer uses the source characters to generate a stream of tokens.
The type of a generated token generally depends on the next source character to
be processed. Similarly, other special behavior of the analyzer depends on
the first source character that hasn't yet been processed.
The following table gives a quick summary of these source characters,
with links to sections that contain more information.</p>
<table class="docutils align-default">
<thead>
<tr class="row-odd"><th class="head"><p>Character</p></th>
<th class="head"><p>Next token (or other relevant documentation)</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><ul class="simple">
<li><p>space</p></li>
<li><p>tab</p></li>
<li><p>formfeed</p></li>
</ul>
</td>
<td><ul class="simple">
<li><p><a class="reference internal" href="#whitespace"><span class="std std-ref">Whitespace</span></a></p></li>
</ul>
</td>
</tr>
<tr class="row-odd"><td><ul class="simple">
<li><p>CR, LF</p></li>
</ul>
</td>
<td><ul class="simple">
<li><p><a class="reference internal" href="#line-structure"><span class="std std-ref">New line</span></a></p></li>
<li><p><a class="reference internal" href="#indentation"><span class="std std-ref">Indentation</span></a></p></li>
</ul>
</td>
</tr>
<tr class="row-even"><td><ul class="simple">
<li><p>backslash (<code class="docutils literal notranslate"><span class="pre">\</span></code>)</p></li>
</ul>
</td>
<td><ul class="simple">
<li><p><a class="reference internal" href="#explicit-joining"><span class="std std-ref">Explicit line joining</span></a></p></li>
<li><p>(Also significant in <a class="reference internal" href="#escape-sequences"><span class="std std-ref">string escape sequences</span></a>)</p></li>
</ul>
</td>
</tr>
<tr class="row-odd"><td><ul class="simple">
<li><p>hash (<code class="docutils literal notranslate"><span class="pre">#</span></code>)</p></li>
</ul>
</td>
<td><ul class="simple">
<li><p><a class="reference internal" href="#comments"><span class="std std-ref">Comment</span></a></p></li>
</ul>
</td>
</tr>
<tr class="row-even"><td><ul class="simple">
<li><p>quote (<code class="docutils literal notranslate"><span class="pre">'</span></code>, <code class="docutils literal notranslate"><span class="pre">"</span></code>)</p></li>
</ul>
</td>
<td><ul class="simple">
<li><p><a class="reference internal" href="#strings"><span class="std std-ref">String literal</span></a></p></li>
</ul>
</td>
</tr>
<tr class="row-odd"><td><ul class="simple">
<li><p>ASCII letter (<code class="docutils literal notranslate"><span class="pre">a</span></code>-<code class="docutils literal notranslate"><span class="pre">z</span></code>, <code class="docutils literal notranslate"><span class="pre">A</span></code>-<code class="docutils literal notranslate"><span class="pre">Z</span></code>)</p></li>
<li><p>non-ASCII character</p></li>
</ul>
</td>
<td><ul class="simple">
<li><p><a class="reference internal" href="#identifiers"><span class="std std-ref">Name</span></a></p></li>
<li><p>Prefixed <a class="reference internal" href="#strings"><span class="std std-ref">string or bytes literal</span></a></p></li>
</ul>
</td>
</tr>
<tr class="row-even"><td><ul class="simple">
<li><p>underscore (<code class="docutils literal notranslate"><span class="pre">_</span></code>)</p></li>
</ul>
</td>
<td><ul class="simple">
<li><p><a class="reference internal" href="#identifiers"><span class="std std-ref">Name</span></a></p></li>
<li><p>(Can also be part of <a class="reference internal" href="#numbers"><span class="std std-ref">numeric literals</span></a>)</p></li>
</ul>
</td>
</tr>
<tr class="row-odd"><td><ul class="simple">
<li><p>number (<code class="docutils literal notranslate"><span class="pre">0</span></code>-<code class="docutils literal notranslate"><span class="pre">9</span></code>)</p></li>
</ul>
</td>
<td><ul class="simple">
<li><p><a class="reference internal" href="#numbers"><span class="std std-ref">Numeric literal</span></a></p></li>
</ul>
</td>
</tr>
<tr class="row-even"><td><ul class="simple">
<li><p>dot (<code class="docutils literal notranslate"><span class="pre">.</span></code>)</p></li>
</ul>
</td>
<td><ul class="simple">
<li><p><a class="reference internal" href="#numbers"><span class="std std-ref">Numeric literal</span></a></p></li>
<li><p><a class="reference internal" href="#operators"><span class="std std-ref">Operator</span></a></p></li>
</ul>
</td>
</tr>
<tr class="row-odd"><td><ul class="simple">
<li><p>question mark (<code class="docutils literal notranslate"><span class="pre">?</span></code>)</p></li>
<li><p>dollar (<code class="docutils literal notranslate"><span class="pre">$</span></code>)</p></li>
<li><p>backquote (<code class="docutils literal notranslate"><span class="pre">`</span></code>)</p>
</li>
<li><p>control character</p></li>
</ul>
</td>
<td><ul class="simple">
<li><p>Error (outside string literals and comments)</p></li>
</ul>
</td>
</tr>
<tr class="row-even"><td><ul class="simple">
<li><p>other printing character</p></li>
</ul>
</td>
<td><ul class="simple">
<li><p><a class="reference internal" href="#operators"><span class="std std-ref">Operator or delimiter</span></a></p></li>
</ul>
</td>
</tr>
<tr class="row-odd"><td><ul class="simple">
<li><p>end of file</p></li>
</ul>
</td>
<td><ul class="simple">
<li><p><a class="reference internal" href="#endmarker-token"><span class="std std-ref">End marker</span></a></p></li>
</ul>
</td>
</tr>
</tbody>
</table>
<section id="line-structure">
<span id="id1"></span><h2><span class="section-number">2.1. </span>Line structure<a class="headerlink" href="#line-structure" title="Link to this heading">¶</a></h2>
<p id="index-1">A Python program is divided into a number of <em>logical lines</em>.</p>
<section id="logical-lines">
<span id="id2"></span><h3><span class="section-number">2.1.1. </span>Logical lines<a class="headerlink" href="#logical-lines" title="Link to this heading">¶</a></h3>
<p id="index-2">The end of a logical line is represented by the token <a class="reference internal" href="../library/token.html#token.NEWLINE" title="token.NEWLINE"><code class="xref py py-data docutils literal notranslate"><span class="pre">NEWLINE</span></code></a>.
Statements cannot cross logical line boundaries except where <code class="xref py py-data docutils literal notranslate"><span class="pre">NEWLINE</span></code>
is allowed by the syntax (e.g., between statements in compound statements).
A logical line is constructed from one or more <em>physical lines</em> by following
the <a class="reference internal" href="#explicit-joining"><span class="std std-ref">explicit</span></a> or <a class="reference internal" href="#implicit-joining"><span class="std std-ref">implicit</span></a>
<em>line joining</em> rules.</p>
</section>
<section id="physical-lines">
<span id="id3"></span><h3><span class="section-number">2.1.2. </span>Physical lines<a class="headerlink" href="#physical-lines" title="Link to this heading">¶</a></h3>
<p>A physical line is a sequence of characters terminated by one the following
end-of-line sequences:</p>
<ul class="simple">
<li><p>the Unix form using ASCII LF (linefeed),</p></li>
<li><p>the Windows form using the ASCII sequence CR LF (return followed by linefeed),</p></li>
<li><p>the '<a class="reference external" href="https://en.wikipedia.org/wiki/Classic_Mac_OS">Classic Mac OS</a>' form using the ASCII CR (return) character.</p>
</li>
</ul>
<p>Regardless of platform, each of these sequences is replaced by a single
ASCII LF (linefeed) character.
(This is done even inside <a class="reference internal" href="#strings"><span class="std std-ref">string literals</span></a>.)
Each line can use any of the sequences; they do not need to be consistent
within a file.</p>
<p>The end of input also serves as an implicit terminator for the final
physical line.</p>
<p>Formally:</p>
<pre class="highlight">
<strong id="grammar-token-python-grammar-newline">newline</strong>: <ASCII LF> | <ASCII CR> <ASCII LF> | <ASCII CR>
</pre>
</section>
<section id="comments">
<span id="id5"></span><h3><span class="section-number">2.1.3. </span>Comments<a class="headerlink" href="#comments" title="Link to this heading">¶</a></h3>
<p id="index-3">A comment starts with a hash character (<code class="docutils literal notranslate"><span class="pre">#</span></code>) that is not part of a string
literal, and ends at the end of the physical line. A comment signifies the end
of the logical line unless the implicit line joining rules are invoked. Comments
are ignored by the syntax.</p>
</section>
<section id="encoding-declarations">
<span id="encodings"></span><h3><span class="section-number">2.1.4. </span>Encoding declarations<a class="headerlink" href="#encoding-declarations" title="Link to this heading">¶</a></h3>
<p id="index-4">If a comment in the first or second line of the Python script matches the
regular expression <code class="docutils literal notranslate"><span class="pre">coding[=:]\s*([-\w.]+)</span></code>, this comment is processed as an
encoding declaration; the first group of this expression names the encoding of
the source code file. The encoding declaration must appear on a line of its
own. If it is the second line, the first line must also be a comment-only line.
The recommended forms of an encoding expression are</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># -*- coding: <encoding-name> -*-</span>
</pre></div>
</div>
<p>which is recognized also by GNU Emacs, and</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># vim:fileencoding=<encoding-name></span>
</pre></div>
</div>
<p>which is recognized by Bram Moolenaar's VIM.</p>
<p>If no encoding declaration is found, the default encoding is UTF-8. If the
implicit or explicit encoding of a file is UTF-8, an initial UTF-8 byte-order
mark (<code class="docutils literal notranslate"><span class="pre">b'\xef\xbb\xbf'</span></code>) is ignored rather than being a syntax error.</p>
<p>If an encoding is declared, the encoding name must be recognized by Python
(see <a class="reference internal" href="../library/codecs.html#standard-encodings"><span class="std std-ref">Standard Encodings</span></a>). The
encoding is used for all lexical analysis, including string literals, comments
and identifiers.</p>
<p id="lexical-source-character">All lexical analysis, including string literals, comments
and identifiers, works on Unicode text decoded using the source encoding.
Any Unicode code point, except the NUL control character, can appear in
Python source.</p>
<pre class="highlight">
<strong id="grammar-token-python-grammar-source_character">source_character</strong>: <any Unicode code point, except NUL>
</pre>
</section>
<section id="explicit-line-joining">
<span id="explicit-joining"></span><h3><span class="section-number">2.1.5. </span>Explicit line joining<a class="headerlink" href="#explicit-line-joining" title="Link to this heading">¶</a></h3>
<p id="index-5">Two or more physical lines may be joined into logical lines using backslash
characters (<code class="docutils literal notranslate"><span class="pre">\</span></code>), as follows: when a physical line ends in a backslash that is
not part of a string literal or comment, it is joined with the following forming
a single logical line, deleting the backslash and the following end-of-line
character. For example:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="mi">1900</span> <span class="o"><</span> <span class="n">year</span> <span class="o"><</span> <span class="mi">2100</span> <span class="ow">and</span> <span class="mi">1</span> <span class="o"><=</span> <span class="n">month</span> <span class="o"><=</span> <span class="mi">12</span> \
<span class="ow">and</span> <span class="mi">1</span> <span class="o"><=</span> <span class="n">day</span> <span class="o"><=</span> <span class="mi">31</span> <span class="ow">and</span> <span class="mi">0</span> <span class="o"><=</span> <span class="n">hour</span> <span class="o"><</span> <span class="mi">24</span> \
<span class="ow">and</span> <span class="mi">0</span> <span class="o"><=</span> <span class="n">minute</span> <span class="o"><</span> <span class="mi">60</span> <span class="ow">and</span> <span class="mi">0</span> <span class="o"><=</span> <span class="n">second</span> <span class="o"><</span> <span class="mi">60</span><span class="p">:</span> <span class="c1"># Looks like a valid date</span>
<span class="k">return</span> <span class="mi">1</span>
</pre></div>
</div>
<p>A line ending in a backslash cannot carry a comment. A backslash does not
continue a comment. A backslash does not continue a token except for string
literals (i.e., tokens other than string literals cannot be split across
physical lines using a backslash). A backslash is illegal elsewhere on a line
outside a string literal.</p>
</section>
<section id="implicit-line-joining">
<span id="implicit-joining"></span><h3><span class="section-number">2.1.6. </span>Implicit line joining<a class="headerlink" href="#implicit-line-joining" title="Link to this heading">¶</a></h3>
<p>Expressions in parentheses, square brackets or curly braces can be split over
more than one physical line without using backslashes. For example:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">month_names</span> <span class="o">=</span> <span class="p">[</span><span class="s1">'Januari'</span><span class="p">,</span> <span class="s1">'Februari'</span><span class="p">,</span> <span class="s1">'Maart'</span><span class="p">,</span> <span class="c1"># These are the</span>
<span class="s1">'April'</span><span class="p">,</span> <span class="s1">'Mei'</span><span class="p">,</span> <span class="s1">'Juni'</span><span class="p">,</span> <span class="c1"># Dutch names</span>
<span class="s1">'Juli'</span><span class="p">,</span> <span class="s1">'Augustus'</span><span class="p">,</span> <span class="s1">'September'</span><span class="p">,</span> <span class="c1"># for the months</span>
<span class="s1">'Oktober'</span><span class="p">,</span> <span class="s1">'November'</span><span class="p">,</span> <span class="s1">'December'</span><span class="p">]</span> <span class="c1"># of the year</span>
</pre></div>
</div>
<p>Implicitly continued lines can carry comments. The indentation of the
continuation lines is not important. Blank continuation lines are allowed.
There is no NEWLINE token between implicit continuation lines. Implicitly
continued lines can also occur within triple-quoted strings (see below); in that
case they cannot carry comments.</p>
</section>
<section id="blank-lines">
<span id="id6"></span><h3><span class="section-number">2.1.7. </span>Blank lines<a class="headerlink" href="#blank-lines" title="Link to this heading">¶</a></h3>
<p id="index-6">A logical line that contains only spaces, tabs, formfeeds and possibly a
comment, is ignored (i.e., no <a class="reference internal" href="../library/token.html#token.NEWLINE" title="token.NEWLINE"><code class="xref py py-data docutils literal notranslate"><span class="pre">NEWLINE</span></code></a> token is generated).
During interactive input of statements, handling of a blank line may differ
depending on the implementation of the read-eval-print loop.
In the standard interactive interpreter, an entirely blank logical line (that
is, one containing not even whitespace or a comment) terminates a multi-line
statement.</p>
</section>
<section id="indentation">
<span id="id7"></span><h3><span class="section-number">2.1.8. </span>Indentation<a class="headerlink" href="#indentation" title="Link to this heading">¶</a></h3>
<p id="index-7">Leading whitespace (spaces and tabs) at the beginning of a logical line is used
to compute the indentation level of the line, which in turn is used to determine
the grouping of statements.</p>
<p>Tabs are replaced (from left to right) by one to eight spaces such that the
total number of characters up to and including the replacement is a multiple of
eight (this is intended to be the same rule as used by Unix). The total number
of spaces preceding the first non-blank character then determines the line's
indentation. Indentation cannot be split over multiple physical lines using
backslashes; the whitespace up to the first backslash determines the
indentation.</p>
<p>Indentation is rejected as inconsistent if a source file mixes tabs and spaces
in a way that makes the meaning dependent on the worth of a tab in spaces; a
<a class="reference internal" href="../library/exceptions.html#TabError" title="TabError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TabError</span></code></a> is raised in that case.</p>
<p><strong>Cross-platform compatibility note:</strong> because of the nature of text editors on
non-UNIX platforms, it is unwise to use a mixture of spaces and tabs for the
indentation in a single source file. It should also be noted that different
platforms may explicitly limit the maximum indentation level.</p>
<p>A formfeed character may be present at the start of the line; it will be ignored
for the indentation calculations above. Formfeed characters occurring elsewhere
in the leading whitespace have an undefined effect (for instance, they may reset
the space count to zero).</p>
<p id="index-8">The indentation levels of consecutive lines are used to generate
<a class="reference internal" href="../library/token.html#token.INDENT" title="token.INDENT"><code class="xref py py-data docutils literal notranslate"><span class="pre">INDENT</span></code></a> and <a class="reference internal" href="../library/token.html#token.DEDENT" title="token.DEDENT"><code class="xref py py-data docutils literal notranslate"><span class="pre">DEDENT</span></code></a> tokens, using a stack,
as follows.</p>
<p>Before the first line of the file is read, a single zero is pushed on the stack;
this will never be popped off again. The numbers pushed on the stack will
always be strictly increasing from bottom to top. At the beginning of each
logical line, the line's indentation level is compared to the top of the stack.
If it is equal, nothing happens. If it is larger, it is pushed on the stack, and
one <code class="xref py py-data docutils literal notranslate"><span class="pre">INDENT</span></code> token is generated. If it is smaller, it <em>must</em> be one of the
numbers occurring on the stack; all numbers on the stack that are larger are
popped off, and for each number popped off a <code class="xref py py-data docutils literal notranslate"><span class="pre">DEDENT</span></code> token is generated.
At the end of the file, a <code class="xref py py-data docutils literal notranslate"><span class="pre">DEDENT</span></code> token is generated for each number
remaining on the stack that is larger than zero.</p>
<p>Here is an example of a correctly (though confusingly) indented piece of Python
code:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span><span class="w"> </span><span class="nf">perm</span><span class="p">(</span><span class="n">l</span><span class="p">):</span>
<span class="c1"># Compute the list of all permutations of l</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">l</span><span class="p">)</span> <span class="o"><=</span> <span class="mi">1</span><span class="p">:</span>
<span class="k">return</span> <span class="p">[</span><span class="n">l</span><span class="p">]</span>
<span class="n">r</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">l</span><span class="p">)):</span>
<span class="n">s</span> <span class="o">=</span> <span class="n">l</span><span class="p">[:</span><span class="n">i</span><span class="p">]</span> <span class="o">+</span> <span class="n">l</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">:]</span>
<span class="n">p</span> <span class="o">=</span> <span class="n">perm</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
<span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">p</span><span class="p">:</span>
<span class="n">r</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">l</span><span class="p">[</span><span class="n">i</span><span class="p">:</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="n">x</span><span class="p">)</span>
<span class="k">return</span> <span class="n">r</span>
</pre></div>
</div>
<p>The following example shows various indentation errors:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span> <span class="k">def</span><span class="w"> </span><span class="nf">perm</span><span class="p">(</span><span class="n">l</span><span class="p">):</span> <span class="c1"># error: first line indented</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">l</span><span class="p">)):</span> <span class="c1"># error: not indented</span>
<span class="n">s</span> <span class="o">=</span> <span class="n">l</span><span class="p">[:</span><span class="n">i</span><span class="p">]</span> <span class="o">+</span> <span class="n">l</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">:]</span>
<span class="n">p</span> <span class="o">=</span> <span class="n">perm</span><span class="p">(</span><span class="n">l</span><span class="p">[:</span><span class="n">i</span><span class="p">]</span> <span class="o">+</span> <span class="n">l</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">:])</span> <span class="c1"># error: unexpected indent</span>
<span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">p</span><span class="p">:</span>
<span class="n">r</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">l</span><span class="p">[</span><span class="n">i</span><span class="p">:</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="n">x</span><span class="p">)</span>
<span class="k">return</span> <span class="n">r</span> <span class="c1"># error: inconsistent dedent</span>
</pre></div>
</div>
<p>(Actually, the first three errors are detected by the parser; only the last
error is found by the lexical analyzer --- the indentation of <code class="docutils literal notranslate"><span class="pre">return</span> <span class="pre">r</span></code> does
not match a level popped off the stack.)</p>
</section>
<section id="whitespace-between-tokens">
<span id="whitespace"></span><h3><span class="section-number">2.1.9. </span>Whitespace between tokens<a class="headerlink" href="#whitespace-between-tokens" title="Link to this heading">¶</a></h3>
<p>Except at the beginning of a logical line or in string literals, the whitespace
characters space, tab and formfeed can be used interchangeably to separate
tokens:</p>
<pre class="highlight">
<strong id="grammar-token-python-grammar-whitespace">whitespace</strong>: <span class="sx">' '</span> | tab | formfeed
</pre>
<p>Whitespace is needed between two tokens only if their concatenation
could otherwise be interpreted as a different token. For example, <code class="docutils literal notranslate"><span class="pre">ab</span></code> is one
token, but <code class="docutils literal notranslate"><span class="pre">a</span> <span class="pre">b</span></code> is two tokens. However, <code class="docutils literal notranslate"><span class="pre">+a</span></code> and <code class="docutils literal notranslate"><span class="pre">+</span> <span class="pre">a</span></code> both produce
two tokens, <code class="docutils literal notranslate"><span class="pre">+</span></code> and <code class="docutils literal notranslate"><span class="pre">a</span></code>, as <code class="docutils literal notranslate"><span class="pre">+a</span></code> is not a valid token.</p>
</section>
<section id="end-marker">
<span id="endmarker-token"></span><h3><span class="section-number">2.1.10. </span>End marker<a class="headerlink" href="#end-marker" title="Link to this heading">¶</a></h3>
<p>At the end of non-interactive input, the lexical analyzer generates an
<a class="reference internal" href="../library/token.html#token.ENDMARKER" title="token.ENDMARKER"><code class="xref py py-data docutils literal notranslate"><span class="pre">ENDMARKER</span></code></a> token.</p>
</section>
</section>
<section id="other-tokens">
<span id="id8"></span><h2><span class="section-number">2.2. </span>Other tokens<a class="headerlink" href="#other-tokens" title="Link to this heading">¶</a></h2>
<p>Besides <a class="reference internal" href="../library/token.html#token.NEWLINE" title="token.NEWLINE"><code class="xref py py-data docutils literal notranslate"><span class="pre">NEWLINE</span></code></a>, <a class="reference internal" href="../library/token.html#token.INDENT" title="token.INDENT"><code class="xref py py-data docutils literal notranslate"><span class="pre">INDENT</span></code></a> and <a class="reference internal" href="../library/token.html#token.DEDENT" title="token.DEDENT"><code class="xref py py-data docutils literal notranslate"><span class="pre">DEDENT</span></code></a>,
the following categories of tokens exist:
<em>identifiers</em> and <em>keywords</em> (<a class="reference internal" href="../library/token.html#token.NAME" title="token.NAME"><code class="xref py py-data docutils literal notranslate"><span class="pre">NAME</span></code></a>), <em>literals</em> (such as
<a class="reference internal" href="../library/token.html#token.NUMBER" title="token.NUMBER"><code class="xref py py-data docutils literal notranslate"><span class="pre">NUMBER</span></code></a> and <a class="reference internal" href="../library/token.html#token.STRING" title="token.STRING"><code class="xref py py-data docutils literal notranslate"><span class="pre">STRING</span></code></a>), and other symbols
(<em>operators</em> and <em>delimiters</em>, <a class="reference internal" href="../library/token.html#token.OP" title="token.OP"><code class="xref py py-data docutils literal notranslate"><span class="pre">OP</span></code></a>).
Whitespace characters (other than logical line terminators, discussed earlier)
are not tokens, but serve to delimit tokens.
Where ambiguity exists, a token comprises the longest possible string that
forms a legal token, when read from left to right.</p>
</section>
<section id="names-identifiers-and-keywords">
<span id="identifiers"></span><h2><span class="section-number">2.3. </span>Names (identifiers and keywords)<a class="headerlink" href="#names-identifiers-and-keywords" title="Link to this heading">¶</a></h2>
<p id="index-9"><a class="reference internal" href="../library/token.html#token.NAME" title="token.NAME"><code class="xref py py-data docutils literal notranslate"><span class="pre">NAME</span></code></a> tokens represent <em>identifiers</em>, <em>keywords</em>, and
<em>soft keywords</em>.</p>
<p>Names are composed of the following characters:</p>
<ul class="simple">
<li><p>uppercase and lowercase letters (<code class="docutils literal notranslate"><span class="pre">A-Z</span></code> and <code class="docutils literal notranslate"><span class="pre">a-z</span></code>),</p></li>
<li><p>the underscore (<code class="docutils literal notranslate"><span class="pre">_</span></code>),</p></li>
<li><p>digits (<code class="docutils literal notranslate"><span class="pre">0</span></code> through <code class="docutils literal notranslate"><span class="pre">9</span></code>), which cannot appear as the first character, and</p></li>
<li><p>non-ASCII characters. Valid names may only contain "letter-like" and
"digit-like" characters; see <a class="reference internal" href="#lexical-names-nonascii"><span class="std std-ref">Non-ASCII characters in names</span></a> for details.</p></li>
</ul>
<p>Names must contain at least one character, but have no upper length limit.
Case is significant.</p>
<p>Formally, names are described by the following lexical definitions:</p>
<pre class="highlight">
<strong id="grammar-token-python-grammar-NAME">NAME</strong>: <a class="reference internal" href="#grammar-token-python-grammar-name_start"><code class="xref docutils literal notranslate"><span class="pre">name_start</span></code></a> <a class="reference internal" href="#grammar-token-python-grammar-name_continue"><code class="xref docutils literal notranslate"><span class="pre">name_continue</span></code></a>*
<strong id="grammar-token-python-grammar-name_start">name_start</strong>: <span class="sx">"a"</span>...<span class="sx">"z"</span> | <span class="sx">"A"</span>...<span class="sx">"Z"</span> | <span class="sx">"_"</span> | <non-ASCII character>
<strong id="grammar-token-python-grammar-name_continue">name_continue</strong>: name_start | <span class="sx">"0"</span>...<span class="sx">"9"</span>
<strong id="grammar-token-python-grammar-identifier">identifier</strong>: <<a class="reference internal" href="#grammar-token-python-grammar-NAME"><code class="xref docutils literal notranslate"><span class="pre">NAME</span></code></a>, except keywords>
</pre>
<p>Note that not all names matched by this grammar are valid; see
<a class="reference internal" href="#lexical-names-nonascii"><span class="std std-ref">Non-ASCII characters in names</span></a> for details.</p>
<section id="keywords">
<span id="id9"></span><h3><span class="section-number">2.3.1. </span>Keywords<a class="headerlink" href="#keywords" title="Link to this heading">¶</a></h3>
<p id="index-10">The following names are used as reserved words, or <em>keywords</em> of the
language, and cannot be used as ordinary identifiers. They must be spelled
exactly as written here:</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>False await else import pass
None break except in raise
True class finally is return
and continue for lambda try
as def from nonlocal while
assert del global not with
async elif if or yield
</pre></div>
</div>
</section>
<section id="soft-keywords">
<span id="id10"></span><h3><span class="section-number">2.3.2. </span>Soft Keywords<a class="headerlink" href="#soft-keywords" title="Link to this heading">¶</a></h3>
<div class="versionadded" id="index-11">
<p><span class="versionmodified added">Added in version 3.10.</span></p>
</div>
<p>Some names are only reserved under specific contexts. These are known as
<em>soft keywords</em>:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">match</span></code>, <code class="docutils literal notranslate"><span class="pre">case</span></code>, and <code class="docutils literal notranslate"><span class="pre">_</span></code>, when used in the <a class="reference internal" href="compound_stmts.html#match"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">match</span></code></a> statement.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">type</span></code>, when used in the <a class="reference internal" href="simple_stmts.html#type"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">type</span></code></a> statement.</p></li>
</ul>
<p>These syntactically act as keywords in their specific contexts,
but this distinction is done at the parser level, not when tokenizing.</p>
<p>As soft keywords, their use in the grammar is possible while still
preserving compatibility with existing code that uses these names as
identifier names.</p>
<div class="versionchanged">
<p><span class="versionmodified changed">تغییر داده شده در نسخه 3.12: </span><code class="docutils literal notranslate"><span class="pre">type</span></code> is now a soft keyword.</p>
</div>
</section>
<section id="reserved-classes-of-identifiers">
<span id="id-classes"></span><span id="index-12"></span><h3><span class="section-number">2.3.3. </span>Reserved classes of identifiers<a class="headerlink" href="#reserved-classes-of-identifiers" title="Link to this heading">¶</a></h3>
<p>Certain classes of identifiers (besides keywords) have special meanings. These
classes are identified by the patterns of leading and trailing underscore
characters:</p>
<dl>
<dt><code class="docutils literal notranslate"><span class="pre">_*</span></code></dt><dd><p>Not imported by <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">module</span> <span class="pre">import</span> <span class="pre">*</span></code>.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">_</span></code></dt><dd><p>In a <code class="docutils literal notranslate"><span class="pre">case</span></code> pattern within a <a class="reference internal" href="compound_stmts.html#match"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">match</span></code></a> statement, <code class="docutils literal notranslate"><span class="pre">_</span></code> is a
<a class="reference internal" href="#soft-keywords"><span class="std std-ref">soft keyword</span></a> that denotes a
<a class="reference internal" href="compound_stmts.html#wildcard-patterns"><span class="std std-ref">wildcard</span></a>.</p>
<p>Separately, the interactive interpreter makes the result of the last evaluation
available in the variable <code class="docutils literal notranslate"><span class="pre">_</span></code>.
(It is stored in the <a class="reference internal" href="../library/builtins.html#module-builtins" title="builtins: The module that provides the built-in namespace."><code class="xref py py-mod docutils literal notranslate"><span class="pre">builtins</span></code></a> module, alongside built-in
functions like <code class="docutils literal notranslate"><span class="pre">print</span></code>.)</p>
<p>Elsewhere, <code class="docutils literal notranslate"><span class="pre">_</span></code> is a regular identifier. It is often used to name
"special" items, but it is not special to Python itself.</p>
<div class="admonition note">
<p class="admonition-title">توجه</p>
<p>The name <code class="docutils literal notranslate"><span class="pre">_</span></code> is often used in conjunction with internationalization;
refer to the documentation for the <a class="reference internal" href="../library/gettext.html#module-gettext" title="gettext: Multilingual internationalization services."><code class="xref py py-mod docutils literal notranslate"><span class="pre">gettext</span></code></a> module for more
information on this convention.</p>
<p>It is also commonly used for unused variables.</p>
</div>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">__*__</span></code></dt><dd><p>System-defined names, informally known as "dunder" names. These names are
defined by the interpreter and its implementation (including the standard library).
Current system names are discussed in the <a class="reference internal" href="datamodel.html#specialnames"><span class="std std-ref">Special method names</span></a> section and elsewhere.
More will likely be defined in future versions of Python. <em>Any</em> use of <code class="docutils literal notranslate"><span class="pre">__*__</span></code> names,
in any context, that does not follow explicitly documented use, is subject to
breakage without warning.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">__*</span></code></dt><dd><p>Class-private names. Names in this category, when used within the context of a
class definition, are re-written to use a mangled form to help avoid name
clashes between "private" attributes of base and derived classes. See section
<a class="reference internal" href="expressions.html#atom-identifiers"><span class="std std-ref">Identifiers (Names)</span></a>.</p>
</dd>
</dl>
</section>
<section id="non-ascii-characters-in-names">
<span id="lexical-names-nonascii"></span><h3><span class="section-number">2.3.4. </span>Non-ASCII characters in names<a class="headerlink" href="#non-ascii-characters-in-names" title="Link to this heading">¶</a></h3>
<p>Names that contain non-ASCII characters need additional normalization
and validation beyond the rules and grammar explained
<a class="reference internal" href="#identifiers"><span class="std std-ref">above</span></a>.
For example, <code class="docutils literal notranslate"><span class="pre">ř_1</span></code>, <code class="docutils literal notranslate"><span class="pre">蛇</span></code>, or <code class="docutils literal notranslate"><span class="pre">साँप</span></code> are valid names, but <code class="docutils literal notranslate"><span class="pre">r〰2</span></code>,
<code class="docutils literal notranslate"><span class="pre">€</span></code>, or <code class="docutils literal notranslate"><span class="pre">🐍</span></code> are not.</p>
<p>This section explains the exact rules.</p>
<p>All names are converted into the <a class="reference external" href="https://www.unicode.org/reports/tr15/#Norm_Forms">normalization form</a> NFKC while parsing.
This means that, for example, some typographic variants of characters are
converted to their "basic" form. For example, <code class="docutils literal notranslate"><span class="pre">fiⁿₐˡᵢᶻₐᵗᵢᵒₙ</span></code> normalizes to
<code class="docutils literal notranslate"><span class="pre">finalization</span></code>, so Python treats them as the same name:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">fiⁿₐˡᵢᶻₐᵗᵢᵒₙ</span> <span class="o">=</span> <span class="mi">3</span>
<span class="gp">>>> </span><span class="n">finalization</span>
<span class="go">3</span>
</pre></div>
</div>
<div class="admonition note">
<p class="admonition-title">توجه</p>
<p>Normalization is done at the lexical level only.
Run-time functions that take names as <em>strings</em> generally do not normalize
their arguments.
For example, the variable defined above is accessible at run time in the
<a class="reference internal" href="../library/functions.html#globals" title="globals"><code class="xref py py-func docutils literal notranslate"><span class="pre">globals()</span></code></a> dictionary as <code class="docutils literal notranslate"><span class="pre">globals()["finalization"]</span></code> but not
<code class="docutils literal notranslate"><span class="pre">globals()["fiⁿₐˡᵢᶻₐᵗᵢᵒₙ"]</span></code>.</p>
</div>
<p>Similarly to how ASCII-only names must contain only letters, digits and
the underscore, and cannot start with a digit, a valid name must
start with a character in the "letter-like" set <code class="docutils literal notranslate"><span class="pre">xid_start</span></code>,
and the remaining characters must be in the "letter- and digit-like" set
<code class="docutils literal notranslate"><span class="pre">xid_continue</span></code>.</p>
<p>These sets are based on the <em>XID_Start</em> and <em>XID_Continue</em> sets as defined by the
Unicode standard annex <a class="reference external" href="https://www.unicode.org/reports/tr31/">UAX-31</a>.
Python's <code class="docutils literal notranslate"><span class="pre">xid_start</span></code> additionally includes the underscore (<code class="docutils literal notranslate"><span class="pre">_</span></code>).
Note that Python does not necessarily conform to <a class="reference external" href="https://www.unicode.org/reports/tr31/">UAX-31</a>.</p>
<p>A non-normative listing of characters in the <em>XID_Start</em> and <em>XID_Continue</em>
sets as defined by Unicode is available in the <a class="reference external" href="https://www.unicode.org/Public/16.0.0/ucd/DerivedCoreProperties.txt">DerivedCoreProperties.txt</a>
file in the Unicode Character Database.
For reference, the construction rules for the <code class="docutils literal notranslate"><span class="pre">xid_*</span></code> sets are given below.</p>
<p>The set <code class="docutils literal notranslate"><span class="pre">id_start</span></code> is defined as the union of:</p>
<ul class="simple">
<li><p>Unicode category <code class="docutils literal notranslate"><span class="pre"><Lu></span></code> - uppercase letters (includes <code class="docutils literal notranslate"><span class="pre">A</span></code> to <code class="docutils literal notranslate"><span class="pre">Z</span></code>)</p></li>
<li><p>Unicode category <code class="docutils literal notranslate"><span class="pre"><Ll></span></code> - lowercase letters (includes <code class="docutils literal notranslate"><span class="pre">a</span></code> to <code class="docutils literal notranslate"><span class="pre">z</span></code>)</p></li>
<li><p>Unicode category <code class="docutils literal notranslate"><span class="pre"><Lt></span></code> - titlecase letters</p></li>
<li><p>Unicode category <code class="docutils literal notranslate"><span class="pre"><Lm></span></code> - modifier letters</p></li>
<li><p>Unicode category <code class="docutils literal notranslate"><span class="pre"><Lo></span></code> - other letters</p></li>
<li><p>Unicode category <code class="docutils literal notranslate"><span class="pre"><Nl></span></code> - letter numbers</p></li>
<li><p>{<code class="docutils literal notranslate"><span class="pre">"_"</span></code>} - the underscore</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre"><Other_ID_Start></span></code> - an explicit set of characters in <a class="reference external" href="https://www.unicode.org/Public/16.0.0/ucd/PropList.txt">PropList.txt</a>
to support backwards compatibility</p></li>
</ul>
<p>The set <code class="docutils literal notranslate"><span class="pre">xid_start</span></code> then closes this set under NFKC normalization, by
removing all characters whose normalization is not of the form
<code class="docutils literal notranslate"><span class="pre">id_start</span> <span class="pre">id_continue*</span></code>.</p>
<p>The set <code class="docutils literal notranslate"><span class="pre">id_continue</span></code> is defined as the union of:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">id_start</span></code> (see above)</p></li>
<li><p>Unicode category <code class="docutils literal notranslate"><span class="pre"><Nd></span></code> - decimal numbers (includes <code class="docutils literal notranslate"><span class="pre">0</span></code> to <code class="docutils literal notranslate"><span class="pre">9</span></code>)</p></li>
<li><p>Unicode category <code class="docutils literal notranslate"><span class="pre"><Pc></span></code> - connector punctuations</p></li>
<li><p>Unicode category <code class="docutils literal notranslate"><span class="pre"><Mn></span></code> - nonspacing marks</p></li>
<li><p>Unicode category <code class="docutils literal notranslate"><span class="pre"><Mc></span></code> - spacing combining marks</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre"><Other_ID_Continue></span></code> - another explicit set of characters in
<a class="reference external" href="https://www.unicode.org/Public/16.0.0/ucd/PropList.txt">PropList.txt</a> to support backwards compatibility</p></li>
</ul>
<p>Again, <code class="docutils literal notranslate"><span class="pre">xid_continue</span></code> closes this set under NFKC normalization.</p>
<p>Unicode categories use the version of the Unicode Character Database as
included in the <a class="reference internal" href="../library/unicodedata.html#module-unicodedata" title="unicodedata: Access the Unicode Database."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unicodedata</span></code></a> module.</p>
<div class="admonition seealso">
<p class="admonition-title">همچنین ملاحظه نمائید</p>
<ul class="simple">
<li><p><span class="target" id="index-13"></span><a class="pep reference external" href="https://peps.python.org/pep-3131/"><strong>PEP 3131</strong></a> -- Supporting Non-ASCII Identifiers</p></li>
<li><p><span class="target" id="index-14"></span><a class="pep reference external" href="https://peps.python.org/pep-0672/"><strong>PEP 672</strong></a> -- Unicode-related Security Considerations for Python</p></li>
</ul>
</div>
</section>
</section>
<section id="literals">
<span id="id11"></span><h2><span class="section-number">2.4. </span>Literals<a class="headerlink" href="#literals" title="Link to this heading">¶</a></h2>
<p id="index-15">Literals are notations for constant values of some built-in types.</p>
<p>In terms of lexical analysis, Python has <a class="reference internal" href="#strings"><span class="std std-ref">string, bytes</span></a>
and <a class="reference internal" href="#numbers"><span class="std std-ref">numeric</span></a> literals.</p>
<p>Other "literals" are lexically denoted using <a class="reference internal" href="#keywords"><span class="std std-ref">keywords</span></a>
(<code class="docutils literal notranslate"><span class="pre">None</span></code>, <code class="docutils literal notranslate"><span class="pre">True</span></code>, <code class="docutils literal notranslate"><span class="pre">False</span></code>) and the special
<a class="reference internal" href="#lexical-ellipsis"><span class="std std-ref">ellipsis token</span></a> (<code class="docutils literal notranslate"><span class="pre">...</span></code>).</p>
</section>
<section id="string-and-bytes-literals">
<span id="strings"></span><span id="index-16"></span><h2><span class="section-number">2.5. </span>String and Bytes literals<a class="headerlink" href="#string-and-bytes-literals" title="Link to this heading">¶</a></h2>
<p>String literals are text enclosed in single quotes (<code class="docutils literal notranslate"><span class="pre">'</span></code>) or double
quotes (<code class="docutils literal notranslate"><span class="pre">"</span></code>). For example:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="s2">"spam"</span>
<span class="s1">'eggs'</span>
</pre></div>
</div>
<p>The quote used to start the literal also terminates it, so a string literal
can only contain the other quote (except with escape sequences, see below).
For example:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="s1">'Say "Hello", please.'</span>
<span class="s2">"Don't do that!"</span>
</pre></div>
</div>
<p>Except for this limitation, the choice of quote character (<code class="docutils literal notranslate"><span class="pre">'</span></code> or <code class="docutils literal notranslate"><span class="pre">"</span></code>)
does not affect how the literal is parsed.</p>
<p>Inside a string literal, the backslash (<code class="docutils literal notranslate"><span class="pre">\</span></code>) character introduces an
<em class="dfn">escape sequence</em>, which has special meaning depending on the character
after the backslash.
For example, <code class="docutils literal notranslate"><span class="pre">\"</span></code> denotes the double quote character, and does <em>not</em> end
the string:</p>
<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="s2">"Say </span><span class="se">\"</span><span class="s2">Hello</span><span class="se">\"</span><span class="s2"> to everyone!"</span><span class="p">)</span>
<span class="go">Say "Hello" to everyone!</span>
</pre></div>
</div>
<p>See <a class="reference internal" href="#escape-sequences"><span class="std std-ref">escape sequences</span></a> below for a full list of such
sequences, and more details.</p>
<section id="triple-quoted-strings">
<span id="index-17"></span><h3><span class="section-number">2.5.1. </span>Triple-quoted strings<a class="headerlink" href="#triple-quoted-strings" title="Link to this heading">¶</a></h3>
<p>Strings can also be enclosed in matching groups of three single or double
quotes.
These are generally referred to as <em class="dfn">triple-quoted strings</em>:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="sd">"""This is a triple-quoted string."""</span>
</pre></div>
</div>
<p>In triple-quoted literals, unescaped quotes are allowed (and are
retained), except that three unescaped quotes in a row terminate the literal,
if they are of the same kind (<code class="docutils literal notranslate"><span class="pre">'</span></code> or <code class="docutils literal notranslate"><span class="pre">"</span></code>) used at the start:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="sd">"""This string has "quotes" inside."""</span>
</pre></div>
</div>
<p>Unescaped newlines are also allowed and retained:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="sd">'''This triple-quoted string</span>
<span class="sd">continues on the next line.'''</span>
</pre></div>
</div>
</section>
<section id="string-prefixes">
<span id="index-18"></span><h3><span class="section-number">2.5.2. </span>String prefixes<a class="headerlink" href="#string-prefixes" title="Link to this heading">¶</a></h3>
<p>String literals can have an optional <em class="dfn">prefix</em> that influences how the
content of the literal is parsed, for example:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="sa">b</span><span class="s2">"data"</span>
<span class="sa">f</span><span class="s1">'</span><span class="si">{</span><span class="n">result</span><span class="si">=}</span><span class="s1">'</span>
</pre></div>
</div>
<p>The allowed prefixes are:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">b</span></code>: <a class="reference internal" href="#bytes-literal"><span class="std std-ref">Bytes literal</span></a></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">r</span></code>: <a class="reference internal" href="#raw-strings"><span class="std std-ref">Raw string</span></a></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">f</span></code>: <a class="reference internal" href="#f-strings"><span class="std std-ref">Formatted string literal</span></a> ("f-string")</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">t</span></code>: <a class="reference internal" href="#t-strings"><span class="std std-ref">Template string literal</span></a> ("t-string")</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">u</span></code>: No effect (allowed for backwards compatibility)</p></li>
</ul>
<p>See the linked sections for details on each type.</p>
<p>Prefixes are case-insensitive (for example, '<code class="docutils literal notranslate"><span class="pre">B</span></code>' works the same as '<code class="docutils literal notranslate"><span class="pre">b</span></code>').
The '<code class="docutils literal notranslate"><span class="pre">r</span></code>' prefix can be combined with '<code class="docutils literal notranslate"><span class="pre">f</span></code>', '<code class="docutils literal notranslate"><span class="pre">t</span></code>' or '<code class="docutils literal notranslate"><span class="pre">b</span></code>', so '<code class="docutils literal notranslate"><span class="pre">fr</span></code>',
'<code class="docutils literal notranslate"><span class="pre">rf</span></code>', '<code class="docutils literal notranslate"><span class="pre">tr</span></code>', '<code class="docutils literal notranslate"><span class="pre">rt</span></code>', '<code class="docutils literal notranslate"><span class="pre">br</span></code>', and '<code class="docutils literal notranslate"><span class="pre">rb</span></code>' are also valid prefixes.</p>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.3: </span>The <code class="docutils literal notranslate"><span class="pre">'rb'</span></code> prefix of raw bytes literals has been added as a synonym
of <code class="docutils literal notranslate"><span class="pre">'br'</span></code>.</p>
<p>Support for the unicode legacy literal (<code class="docutils literal notranslate"><span class="pre">u'value'</span></code>) was reintroduced
to simplify the maintenance of dual Python 2.x and 3.x codebases.
See <span class="target" id="index-19"></span><a class="pep reference external" href="https://peps.python.org/pep-0414/"><strong>PEP 414</strong></a> for more information.</p>
</div>
</section>
<section id="formal-grammar">
<h3><span class="section-number">2.5.3. </span>Formal grammar<a class="headerlink" href="#formal-grammar" title="Link to this heading">¶</a></h3>
<p>String literals, except <a class="reference internal" href="#f-strings"><span class="std std-ref">"f-strings"</span></a> and
<a class="reference internal" href="#t-strings"><span class="std std-ref">"t-strings"</span></a>, are described by the
following lexical definitions.</p>
<p>These definitions use <a class="reference internal" href="introduction.html#lexical-lookaheads"><span class="std std-ref">negative lookaheads</span></a> (<code class="docutils literal notranslate"><span class="pre">!</span></code>)
to indicate that an ending quote ends the literal.</p>
<pre class="highlight">
<strong id="grammar-token-python-grammar-STRING">STRING</strong>: [<a class="reference internal" href="#grammar-token-python-grammar-stringprefix"><code class="xref docutils literal notranslate"><span class="pre">stringprefix</span></code></a>] (<a class="reference internal" href="#grammar-token-python-grammar-stringcontent"><code class="xref docutils literal notranslate"><span class="pre">stringcontent</span></code></a>)
<strong id="grammar-token-python-grammar-stringprefix">stringprefix</strong>: <(<span class="sx">"r"</span> | <span class="sx">"u"</span> | <span class="sx">"b"</span> | <span class="sx">"br"</span> | <span class="sx">"rb"</span>), case-insensitive>
<strong id="grammar-token-python-grammar-stringcontent">stringcontent</strong>:
| <span class="sx">"'''"</span> ( !<span class="sx">"'''"</span> <a class="reference internal" href="#grammar-token-python-grammar-longstringitem"><code class="xref docutils literal notranslate"><span class="pre">longstringitem</span></code></a>)* <span class="sx">"'''"</span>
| <span class="sx">'"""'</span> ( !<span class="sx">'"""'</span> <a class="reference internal" href="#grammar-token-python-grammar-longstringitem"><code class="xref docutils literal notranslate"><span class="pre">longstringitem</span></code></a>)* <span class="sx">'"""'</span>
| <span class="sx">"'"</span> ( !<span class="sx">"'"</span> <a class="reference internal" href="#grammar-token-python-grammar-stringitem"><code class="xref docutils literal notranslate"><span class="pre">stringitem</span></code></a>)* <span class="sx">"'"</span>
| <span class="sx">'"'</span> ( !<span class="sx">'"'</span> <a class="reference internal" href="#grammar-token-python-grammar-stringitem"><code class="xref docutils literal notranslate"><span class="pre">stringitem</span></code></a>)* <span class="sx">'"'</span>
<strong id="grammar-token-python-grammar-stringitem">stringitem</strong>: <a class="reference internal" href="#grammar-token-python-grammar-stringchar"><code class="xref docutils literal notranslate"><span class="pre">stringchar</span></code></a> | <a class="reference internal" href="#grammar-token-python-grammar-stringescapeseq"><code class="xref docutils literal notranslate"><span class="pre">stringescapeseq</span></code></a>
<strong id="grammar-token-python-grammar-stringchar">stringchar</strong>: <any <a class="reference internal" href="#grammar-token-python-grammar-source_character"><code class="xref docutils literal notranslate"><span class="pre">source_character</span></code></a>, except backslash and newline>
<strong id="grammar-token-python-grammar-longstringitem">longstringitem</strong>: <a class="reference internal" href="#grammar-token-python-grammar-stringitem"><code class="xref docutils literal notranslate"><span class="pre">stringitem</span></code></a> | newline
<strong id="grammar-token-python-grammar-stringescapeseq">stringescapeseq</strong>: <span class="sx">"\"</span> <any <a class="reference internal" href="#grammar-token-python-grammar-source_character"><code class="xref docutils literal notranslate"><span class="pre">source_character</span></code></a>>
</pre>
<p>Note that as in all lexical definitions, whitespace is significant.
In particular, the prefix (if any) must be immediately followed by the starting
quote.</p>
</section>
<section id="escape-sequences">
<span id="index-20"></span><span id="id12"></span><h3><span class="section-number">2.5.4. </span>Escape sequences<a class="headerlink" href="#escape-sequences" title="Link to this heading">¶</a></h3>
<p>Unless an '<code class="docutils literal notranslate"><span class="pre">r</span></code>' or '<code class="docutils literal notranslate"><span class="pre">R</span></code>' prefix is present, escape sequences in string and
bytes literals are interpreted according to rules similar to those used by
Standard C. The recognized escape sequences are:</p>
<table class="docutils align-default">
<thead>
<tr class="row-odd"><th class="head"><p>Escape Sequence</p></th>
<th class="head"><p>Meaning</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">\</span></code><newline></p></td>
<td><p><a class="reference internal" href="#string-escape-ignore"><span class="std std-ref">Ignored end of line</span></a></p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">\\</span></code></p></td>
<td><p><a class="reference internal" href="#string-escape-escaped-char"><span class="std std-ref">Backslash</span></a></p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">\'</span></code></p></td>
<td><p><a class="reference internal" href="#string-escape-escaped-char"><span class="std std-ref">Single quote</span></a></p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">\"</span></code></p></td>
<td><p><a class="reference internal" href="#string-escape-escaped-char"><span class="std std-ref">Double quote</span></a></p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">\a</span></code></p></td>
<td><p>ASCII Bell (BEL)</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">\b</span></code></p></td>
<td><p>ASCII Backspace (BS)</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">\f</span></code></p></td>
<td><p>ASCII Formfeed (FF)</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">\n</span></code></p></td>
<td><p>ASCII Linefeed (LF)</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">\r</span></code></p></td>
<td><p>ASCII Carriage Return (CR)</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">\t</span></code></p></td>
<td><p>ASCII Horizontal Tab (TAB)</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">\v</span></code></p></td>
<td><p>ASCII Vertical Tab (VT)</p></td>
</tr>
<tr class="row-odd"><td><p><code class="samp docutils literal notranslate"><span class="pre">\</span><em><span class="pre">ooo</span></em></code></p></td>
<td><p><a class="reference internal" href="#string-escape-oct"><span class="std std-ref">Octal character</span></a></p></td>
</tr>
<tr class="row-even"><td><p><code class="samp docutils literal notranslate"><span class="pre">\x</span><em><span class="pre">hh</span></em></code></p></td>
<td><p><a class="reference internal" href="#string-escape-hex"><span class="std std-ref">Hexadecimal character</span></a></p></td>
</tr>
<tr class="row-odd"><td><p><code class="samp docutils literal notranslate"><span class="pre">\N{</span><em><span class="pre">name</span></em><span class="pre">}</span></code></p></td>
<td><p><a class="reference internal" href="#string-escape-named"><span class="std std-ref">Named Unicode character</span></a></p></td>
</tr>
<tr class="row-even"><td><p><code class="samp docutils literal notranslate"><span class="pre">\u</span><em><span class="pre">xxxx</span></em></code></p></td>
<td><p><a class="reference internal" href="#string-escape-long-hex"><span class="std std-ref">Hexadecimal Unicode character</span></a></p></td>
</tr>
<tr class="row-odd"><td><p><code class="samp docutils literal notranslate"><span class="pre">\U</span><em><span class="pre">xxxxxxxx</span></em></code></p></td>
<td><p><a class="reference internal" href="#string-escape-long-hex"><span class="std std-ref">Hexadecimal Unicode character</span></a></p></td>
</tr>
</tbody>
</table>
<section id="ignored-end-of-line">
<span id="string-escape-ignore"></span><h4><span class="section-number">2.5.4.1. </span>Ignored end of line<a class="headerlink" href="#ignored-end-of-line" title="Link to this heading">¶</a></h4>
<p>A backslash can be added at the end of a line to ignore the newline:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="s1">'This string will not include </span><span class="se">\</span>
<span class="gp">... </span><span class="s1">backslashes or newline characters.'</span>
<span class="go">'This string will not include backslashes or newline characters.'</span>
</pre></div>
</div>
<p>The same result can be achieved using <a class="reference internal" href="#strings"><span class="std std-ref">triple-quoted strings</span></a>,
or parentheses and <a class="reference internal" href="expressions.html#string-concatenation"><span class="std std-ref">string literal concatenation</span></a>.</p>
</section>
<section id="escaped-characters">
<span id="string-escape-escaped-char"></span><h4><span class="section-number">2.5.4.2. </span>Escaped characters<a class="headerlink" href="#escaped-characters" title="Link to this heading">¶</a></h4>
<p>To include a backslash in a non-<a class="reference internal" href="#raw-strings"><span class="std std-ref">raw</span></a> Python string
literal, it must be doubled. The <code class="docutils literal notranslate"><span class="pre">\\</span></code> escape sequence denotes a single
backslash character:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="s1">'C:</span><span class="se">\\</span><span class="s1">Program Files'</span><span class="p">)</span>
<span class="go">C:\Program Files</span>
</pre></div>
</div>
<p>Similarly, the <code class="docutils literal notranslate"><span class="pre">\'</span></code> and <code class="docutils literal notranslate"><span class="pre">\"</span></code> sequences denote the single and double
quote character, respectively:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="s1">'</span><span class="se">\'</span><span class="s1"> and </span><span class="se">\"</span><span class="s1">'</span><span class="p">)</span>
<span class="go">' and "</span>
</pre></div>
</div>
</section>
<section id="octal-character">
<span id="string-escape-oct"></span><h4><span class="section-number">2.5.4.3. </span>Octal character<a class="headerlink" href="#octal-character" title="Link to this heading">¶</a></h4>
<p>The sequence <code class="samp docutils literal notranslate"><span class="pre">\</span><em><span class="pre">ooo</span></em></code> denotes a <em>character</em> with the octal (base 8)
value <em>ooo</em>:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="s1">'</span><span class="se">\120</span><span class="s1">'</span>
<span class="go">'P'</span>