name = "Eric"
age = 74
"Hello, %s. You are %s." % (name, age)
"Hello, {}. You are {}.".format(name, age)
available after Python version 3.6
name = "Eric"
age = 74
f"Hello, {name}. You are {age}."
It indicated what type is being formated. For examle d for integer and f for floating point
f'{17.489:.2f}'
e
presentation type for exponential (scientific) notation.f'Scientific Notation : {17000.489:e}'
b
, o
and x
or X
) that format integers using the binary, octal or hexadecimal number systems. f'{10:d}'
f'{65:c} {97:c}'
s
specified explicitly, the value to format must be a string, an expression that produces a string or a string literal. f'{"hello":s} {7}'
Decimal
values, Exponential (scientific) notation can be used to format the values more compactlyfrom decimal import Decimal
f'{Decimal("10000000000000000000000000.0"):.3f}'
f'{Decimal("10000000000000000000000000.0"):.3e}'
1.000e+25
is equivalent to 1.000 x 1025
E
for the exponent, use the E
presentation type rather than e
.©1992–2020 by Pearson Education, Inc. All Rights Reserved. This content is based on Chapter 5 of the book Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and the Cloud.
DISCLAIMER: The authors and publisher of this book have used their best efforts in preparing the book. These efforts include the development, research, and testing of the theories and programs to determine their effectiveness. The authors and publisher make no warranty of any kind, expressed or implied, with regard to these programs or to the documentation contained in these books. The authors and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs.