@@ -551,82 +551,52 @@ The documentation for the `find` method is available at
551
551
552
552
< https://docs.python.org/library/stdtypes.html#string-methods > .
553
553
554
- Format operator
555
- ---------------
554
+ Formatted String Literals
555
+ -------------------------
556
556
557
- \index{format operator}
558
- \index{operator!format}
557
+ \index{formatted string literals}
559
558
560
- The * format operator* , ` % ` allows us to
561
- construct strings, replacing parts of the strings with the data stored
562
- in variables. When applied to integers, ` % ` is the modulus
563
- operator. But when the first operand is a string, ` % ` is the
564
- format operator.
565
-
566
- \index{format string}
559
+ A formatted string literal (often referred to simply as an f-string)
560
+ allows Python expressions to be used within string literals. This is
561
+ accomplished by prepending an ` f ` to the string literal and enclosing
562
+ expressions in curly braces ` {} ` .
567
563
568
- The first operand is the * format string* , which contains
569
- one or more * format sequences* that specify how the
570
- second operand is formatted. The result is a string.
571
-
572
- \index{format sequence}
573
-
574
- For example, the format sequence ` %d ` means that the second operand
575
- should be formatted as an integer ("d" stands for "decimal"):
564
+ For example, wrapping a variable name in curly braces inside an
565
+ f-string will cause it to be replaced by its value:
576
566
577
567
~~~~ {.python}
578
568
>>> camels = 42
579
- >>> '%d' % camels
569
+ >>> f'{ camels}'
580
570
'42'
581
571
~~~~
582
572
583
573
The result is the string '42', which is not to be confused with the
584
574
integer value 42.
585
575
586
- A format sequence can appear anywhere in the string, so you can embed a
576
+ An expression can appear anywhere in the string, so you can embed a
587
577
value in a sentence:
588
578
589
579
~~~~ {.python}
590
580
>>> camels = 42
591
- >>> 'I have spotted %d camels.' % camels
581
+ >>> f 'I have spotted {camels} camels.'
592
582
'I have spotted 42 camels.'
593
583
~~~~
594
584
595
- If there is more than one format sequence in the string, the second
596
- argument has to be a tuple^[ A tuple is a sequence of comma-separated
597
- values inside a pair of parenthesis. We will cover tuples in Chapter 10] .
598
- Each format sequence is matched with an
599
- element of the tuple, in order.
600
-
601
- The following example uses ` %d ` to format an integer, ` %g ` to format
602
- a floating-point number (don't ask why), and ` %s ` to format a string:
585
+ Several expressions can be included within a single string literal
586
+ in order to create more complex strings.
603
587
604
588
~~~~ {.python}
605
- >>> 'In %d years I have spotted %g %s.' % (3, 0.1, 'camels')
589
+ >>> years = 3
590
+ >>> count = .1
591
+ >>> species = 'camels'
592
+ >>> f'In {years} years I have spotted {count} {species}.'
606
593
'In 3 years I have spotted 0.1 camels.'
607
594
~~~~
608
595
609
- The number of elements in the tuple must match the number of format
610
- sequences in the string. The types of the elements also must match the
611
- format sequences:
612
-
613
- \index{exception!TypeError}
614
- \index{TypeError}
615
-
616
- ~~~~ {.python}
617
- >>> '%d %d %d' % (1, 2)
618
- TypeError: not enough arguments for format string
619
- >>> '%d' % 'dollars'
620
- TypeError: %d format: a number is required, not str
621
- ~~~~
622
-
623
- In the first example, there aren't enough elements; in the second, the
624
- element is the wrong type.
625
-
626
- The format operator is powerful, but it can be difficult to use. You can
627
- read more about it at
596
+ Formatted string literals are powerful, and they can do even more than is covered
597
+ here. You can read more about them at
628
598
629
- < https://docs.python.org/library/stdtypes .html#printf-style- string-formatting > .
599
+ < https://docs.python.org/3/tutorial/inputoutput .html#formatted- string-literals > .
630
600
631
601
Debugging
632
602
---------
0 commit comments