7.11. Format operator¶
The format operator, %
allows us to
construct strings, replacing parts of the strings with the data stored
in variables. When applied to integers, %
is the modulus
operator. But when the first operand is a string, %
is the
format operator.
- division
- Incorrect! The division operator is '/'. Try again.
- modulus (remainder)
- Correct! % can be used to find the remainder.
- format
- Correct! % can be used to specify how string is formatted.
- conversion
- Incorrect! Conversion uses "int", "str", or other functions. Try again.
11-9-1: The % operator is used for which purposes? Select all that apply.
Before you keep reading...
Runestone Academy can only continue if we get support from individuals like you. As a student you are well aware of the high cost of textbooks. Our mission is to provide great books to you for free, but we ask that you consider a $10 donation, more if you can or less if $10 is a burden.
The first operand is the format string, which contains one or more format sequences that specify how the second operand is formatted. The result is a string.
For example, the format sequence %d
means that the second operand
should be formatted as an integer (“d” stands for “decimal”):
>>>camels = 42
>>>print('%d' % camels)
42
The result is the string “42”, which is not to be confused with the integer value 42.
A format sequence can appear anywhere in the string, so you can embed a value in a sentence:
If there is more than one format sequence in the string, the second argument has to be a tuple [A tuple is a sequence of comma-separated values inside a pair of parentheses. We will cover tuples in Chapter 10]. Each format sequence is matched with an element of the tuple, in order.
The following example uses %d
to format an integer, %g
to format
a floating-point number (don’t ask why), and %s
to format a string:
The number of elements in the tuple must match the number of format sequences in the string. The types of the elements also must match the format sequences:
>>> '%d %d %d' % (1, 2)
TypeError: not enough arguments for format string
>>> '%d' % 'dollars'
TypeError: %d format: a number is required, not str
In the first example, there aren’t enough elements; in the second, the element is the wrong type.
The format operator is powerful, but it can be difficult to use. You can read more about it at
https://docs.python.org/library/stdtypes.html#printf-style-string-formatting.
- string
- Incorrect! %s formats strings. Try again.
- integer
- Correct! %d formats integers.
- float
- Incorrect! %g formats floating point numbers. Try again.
11-9-4: What does the %d operator format?
- string
- Incorrect! %s formats strings. Try again.
- integer
- Incorrect! %d formats integers. Try again.
- float
- Correct! %g formats floating point numbers.
11-9-5: What does the %g operator format?
- string
- Correct! %s formats strings.
- integer
- Incorrect! %d formats integers. Try again.
- float
- Incorrect! %g formats floating point numbers. Try again.
11-9-6: What does the %s operator format?