Foundations Updated 2026-09 View as Markdown

String Formatting

Python uses C-style string formatting to create new, formatted strings.

Python uses C-style string formatting to create new, formatted strings. The “%” operator is used to format a set of variables enclosed in a “tuple” (a fixed size list), together with a format string, which contains normal text together with “argument specifiers”, special symbols like “%s” and “%d”.

Let’s say you have a variable called “name” with your user name in it, and you would then like to print(out a greeting to that user.)

python
# This prints out "Hello, John!"
name = "John"
print("Hello, %s!" % name)

To use two or more argument specifiers, use a tuple (parentheses):

python
# This prints out "John is 23 years old."
name = "John"
age = 23
print("%s is %d years old." % (name, age))

Any object which is not a string can be formatted using the %s operator as well. The string which returns from the “repr” method of that object is formatted as the string. For example:

python
# This prints out: A list: [1, 2, 3]
mylist = [1,2,3]
print("A list: %s" % mylist)

Here are some basic argument specifiers you should know:

%s - String (or any object with a string representation, like numbers)

%d - Integers

%f - Floating point numbers

%.f - Floating point numbers with a fixed amount of digits to the right of the dot.

%x/%X - Integers in hex representation (lowercase/uppercase)

Exercise

Try it

ready

Get the Python agent pack

A battle-tested AGENTS.md, the review checklist, and the failure-mode cheat sheet for Python. One email, then occasional updates when the tooling shifts. No course pitch.

Unsubscribe in one click. We never sell the list. Or just take the AGENTS.md now — no email needed.