Skip to content

Commit 5c63f34

Browse files
authored
Merge pull request #424 from jncraton/patch-5
Replace format operator with f-strings
2 parents 482ed03 + 60b6720 commit 5c63f34

File tree

1 file changed

+21
-51
lines changed

1 file changed

+21
-51
lines changed

book3/06-strings.mkd

+21-51
Original file line numberDiff line numberDiff line change
@@ -551,82 +551,52 @@ The documentation for the `find` method is available at
551551

552552
<https://docs.python.org/library/stdtypes.html#string-methods>.
553553

554-
Format operator
555-
---------------
554+
Formatted String Literals
555+
-------------------------
556556

557-
\index{format operator}
558-
\index{operator!format}
557+
\index{formatted string literals}
559558

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 `{}`.
567563

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:
576566

577567
~~~~ {.python}
578568
>>> camels = 42
579-
>>> '%d' % camels
569+
>>> f'{camels}'
580570
'42'
581571
~~~~
582572

583573
The result is the string '42', which is not to be confused with the
584574
integer value 42.
585575

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
587577
value in a sentence:
588578

589579
~~~~ {.python}
590580
>>> camels = 42
591-
>>> 'I have spotted %d camels.' % camels
581+
>>> f'I have spotted {camels} camels.'
592582
'I have spotted 42 camels.'
593583
~~~~
594584

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.
603587

604588
~~~~ {.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}.'
606593
'In 3 years I have spotted 0.1 camels.'
607594
~~~~
608595

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
628598

629-
<https://docs.python.org/library/stdtypes.html#printf-style-string-formatting>.
599+
<https://docs.python.org/3/tutorial/inputoutput.html#formatted-string-literals>.
630600

631601
Debugging
632602
---------

0 commit comments

Comments
 (0)