Книга: Practical Programming, Fourth Edition
Назад: Getting Information from the Keyb oard
Дальше: Quotes About Strings

Formatted Strings

It is common in text processing to construct a string using a template and specific values to fill into it. For example, you may have a generic birthday congratulations, such as "Happy AGEth birthday, NAME!", which you want to customize by specifying the congratulant’s name and age.

Python historically provides four mechanisms for such customization, listed below in the order of recency and usability:

  1. String concatenation

  2. Substitution operator

  3. str.format method

  4. Formatted strings

Formatted strings (f-strings) are an efficient way to construct text

We’ll examine only the formatted strings, which are the newest and generally the most concise and efficient way to format text in modern Python. A formatted string, also known as an f-string, is essentially a template string with additional marks denoting placeholders and indicating what and how to fill them in. An f-string starts with the letter f (or F) before the opening quotation mark. These f-strings without placeholders, once read by Python, are indistinguishable from “normal” strings:

 >>> f​'abc'
 'abc'
 >>> F​"abc"
 'abc'

A placeholder is any valid Python expression enclosed in braces. When a string is read, each placeholder is replaced with the value of the enclosed expression. If necessary, the value of the expression is automatically converted to a string.

 >>> name = ​'Paul Newman'
 >>> age = 100
 >>> f​'Happy birthday, {name}!'
 'Happy birthday, Paul Newman!'
 >>> ​'Happy birthday, {name}!'​ ​# Not an f-string!
 'Happy birthday, {name}!'
 >>> f​'Happy {age}th birthday, {name}!'
 'Happy 100th birthday, Paul Newman!'

Next year, you may update the variable age or compute the new age within the f-string (the th suffix requires special treatment, because it may become st, nd, or even rd):

 >>> f​'Happy {age+1}th birthday, {name}!'
 'Happy 101th birthday, Paul Newman!'

If you want to use braces within an f-string, you must double each brace: {{ and }}. Python will print them as { and }, and not treat them as placeholders:

 >>> stuff = ​'braces'
 >>> f​'stuff, {stuff}, and more {{stuff}}'
 'stuff, braces, and more {stuff}'

In addition to the filler expression, a placeholder may contain a format specifier, which provides additional information about formatting the expression. A colon (:) separates the expression from the format specifier. A simple format specifier resembles a floating-point number, where the “integer” part defines the number of positions reserved for the result, and the decimal part, if present, determines the number of text symbols or decimal digits to display:

 >>> f​'Name: {name}.'​ ​# No format specifier
 'Name: Paul Newman.'
 >>> f​'Name: {name:20}.'
 'Name: Paul Newman .'
 >>> f​'Name: {name:20.4}.'​ ​# Truncated
 'Name: Paul .'
 >>> f​'Age: {age}.'
 'Age: 100.'
 >>> f​'Age: {age:20}.'
 'Age: 100.'
 >>> f​'Age: {age:20.2f}.'
 'Age: 100.00.'

An extra f at the end of the format specifier indicates floating-point formatting.

You can insert various control symbols in front of the format specifier, such as alignment: < (left-align the result; default for strings), ^ (center the result), > (right-align the result; default for numbers).

 >>> f​'Name: {name:<20}.'
 'Name: Paul Newman .'
 >>> f​'Name: {name:^20}.'
 'Name: Paul Newman .'
 >>> f​'Name: {name:>20}.'
 'Name: Paul Newman.'

Please explore more control symbols by reading Python documentation.

Назад: Getting Information from the Keyb oard
Дальше: Quotes About Strings