Formatted Strings#

RealPython Reference
Python docs Reference

%-formatting (old)#

This style of formatted print is old and not recommended. But, you may see it somewhere. The % acts like a “escape sequence” similar to how it is done in Java’s System.out.printf.

The string literal is the format string. After the string literal the arguments to the string is separated by %. I find this odd. Apparently, so did many other programmers which is why this formatting isn’t used very much these days.

print("Hello, %s. You are %s." % (name, adjective))

format method#

There is a method named format on the string object. The string contains formatting information inside of curly braces. This formatting is described below. The string can contain many curly braces and other text to be printed.

Once again, this method of formatting strings is not used very much these days.

n = 2.154327
format_float = "{:.2f}".format(n)

print("text {0}".format('value'))


This is the most recent and popular way to format a string and values. Python’s string formatting functionality is built into Python’s f-string. The API is a bit complicated to understand. It is similar to the way Java implements printf, only it has different escape sequences and specifics. In short, when a string literal is preceded with 'f' then the string is expecting to have some formatting sequence in it. A formatting sequence is embedded inside curly braces {} and the text inside the curly braces is interpreted. It would contain identifiers, method calls, and possibly formatting options following a colon. Here is the syantax for printing a floating point number with some text label:


For example, let’s say we do the following:

gpa = 3.899

That code results in printing, GPA=3.90 (due to rounding to 2 decimal places).

There are lots and lots of options that you can find in Python docs Reference.


n = 30
print(f'Hex     = {n:05x}')
print(f'Decimal = {n:5d}')
print(f'SciNota = {n:e}')
print(f'Percent = {n:%}')

# assume foo()=8
from math import sqrt
print(f'Foo = {foo()} and squirt 2 = {sqrt(2):.3f}')


Hex     = 0001e
Decimal =    30
Sci Not = 3.000000e+01
Percent = 3000.000000%
Foo = 8 and squirt 2 = 1.414