1>>> name = "Fred"
2>>> f"He said his name is {name!r}."
3"He said his name is 'Fred'."
4>>> f"He said his name is {repr(name)}." # repr() is equivalent to !r
5"He said his name is 'Fred'."
6>>> width = 10
7>>> precision = 4
8>>> value = decimal.Decimal("12.34567")
9>>> f"result: {value:{width}.{precision}}" # nested fields
10'result: 12.35'
11>>> today = datetime(year=2017, month=1, day=27)
12>>> f"{today:%B %d, %Y}" # using date format specifier
13'January 27, 2017'
14>>> f"{today=:%B %d, %Y}" # using date format specifier and debugging
15'today=January 27, 2017'
16>>> number = 1024
17>>> f"{number:#0x}" # using integer format specifier
18'0x400'
19>>> foo = "bar"
20>>> f"{ foo = }" # preserves whitespace
21" foo = 'bar'"
22>>> line = "The mill's closed"
23>>> f"{line = }"
24'line = "The mill\'s closed"'
25>>> f"{line = :20}"
26"line = The mill's closed "
27>>> f"{line = !r:20}"
28'line = "The mill\'s closed" '
1>>> "Hello, {1}. You are {0}.".format(age, name)
2'Hello, Eric. You are 74.'
3
1>>> first_name = "Eric"
2>>> last_name = "Idle"
3>>> age = 74
4>>> profession = "comedian"
5>>> affiliation = "Monty Python"
6>>> print(("Hello, {first_name} {last_name}. You are {age}. " +
7>>> "You are a {profession}. You were a member of {affiliation}.") \
8>>> .format(first_name=first_name, last_name=last_name, age=age, \
9>>> profession=profession, affiliation=affiliation))
10'Hello, Eric Idle. You are 74. You are a comedian. You were a member of Monty Python.'
11