Python: Formatted Output

Definition:This refers to the process of embedding data (such as variables, results of expressions, etc.) from a program into a string in a specific format and outputting that string.

Methods for achieving formatted output:

% Signformatted output

The % sign formatting string method has existed since the inception of Python. In the string, the position where data needs to be passed is first occupied by % (s, d, f), followed by a connecting % at the end of the string, along with the corresponding value.

%d: occupies an integer type, only accepts numeric types; other types cannot be converted to integers and will throw an error; if the value is a decimal, only the integer part is retained without rounding;

For example:

Input:print ( “小明’s Python score is %d” % 99.5 )

Output:小明’s Python score is 99

%f: occupies a floating-point type, only accepts numeric types; other types cannot be converted to integers and will throw an error (default retains 6 decimal places). For multiple values, they must be enclosed in parentheses and filled in according to the order of placeholders.

For example:

Input:print (“小明’s Python score is %f” % 99.5 )

Output:小明’s Python score is 99.500000

%s: occupies a string, can accept any type of value passed in and convert it to a string;

For example:

Input:name = “小明”

grade = 1

Python = 90

print ( “%s’s class is %d, Python score is %.2f” % ( name, grade, Python )

Output:小明’s class is 1, Python score is 90.00

Note: When occupying multiple data, the data must be enclosed in parentheses and filled in according to the order of placeholders; the number of values in the parentheses must match the number of occupied positions, otherwise an error will occur; the filled content can include not only variables but also other types of data or expressions; disadvantage: when there are too many characters to format, the positions can easily become confused;

Format Function for Formatted Output

Syntax:“Content { } Content”.format(value)

The format function was introduced in Python 2.6, and is a new string formatting method intended to replace the traditional % formatting method.

f-string Formatted Output

Syntax:f/F”Content {content to be occupied} Content”

This feature was introduced in Python 3.6, starting with F or f, followed by a string, where values in the string are enclosed in curly braces, and it will replace them with the computed values of variables or expressions.

For example:

Input:shop = “Green Leaf Fruit”

date = “October 27, 2025”

balance = 50

top-up = 200

address = “Haidian District, Beijing.”

print ( f “【{shop} Store】Dear user, hello: Congratulations on successfully recharging {top-up} yuan on {date}, your account balance is {balance} yuan, every employee in our store is dedicated to serving you, address: {address} ” )

Output:【Green Leaf Fruit Store】Dear user, hello: Congratulations on successfully recharging 200 yuan on October 27, 2025, your account balance is 50 yuan, every employee in our store is dedicated to serving you, address: Haidian District, Beijing.

Characteristics of f-string Formatted Output:

Using regular output can be inconvenient in some cases; using f-string formatting is simple and quick; f-string does not consider types; whatever is filled in is what is output; it allows control over width and precision of data; the filled content can be any type of data, including expressions; { } cannot contain \, otherwise an error will occur.

Comparison of Formatted Input and Regular Input Methods:

year = 2025

month = 10

day = 27

Regular Input:print ( “Today is”, year, “year”, month, “month”, day, “day”, sep = “” )

% Formatted Input:print (“Today is %d year %d month %d day” % ( year, month, day ))

f-string Formatted Input:print ( f”Today is { year } year { month } month { day } day”)

Control of Formatting Precision

Use the auxiliary symbols “m.n” to control the width and precision of data;

m: controls the width, must be a number; setting the width must be less than the number itself, otherwise it will not take effect; when the width of the data itself is less than the set width, the output result will be padded with spaces in front;

n: controls the decimal precision, must be a number, rounding is applied to the decimal part.

%d integer type data can only control width, while %f floating-point data can control both width and precision;

For example (using % formatted output):

Input 1:

print ( “%5d” % 100 )

Output 1: 100

Input 2:

print ( “%5.2f ” % 12345678 )

Output 2:12345678.00

For example (using f-string formatted output):

Input:shop = “Green Leaf Fruit”

date = “October 27, 2025”

balance = 50

top-up = 200

address = “Haidian District, Beijing.”

print ( f”【{ shop } Store】Dear user, hello: Congratulations on successfully recharging { top-up : .2f } yuan, your account balance is { balance : .2f } yuan, every employee in our store is dedicated to serving you, address: { address } ” )

Output:【Green Leaf Fruit Store】Dear user, hello: Congratulations on successfully recharging 200.00 yuan, your account balance is 50.00 yuan, every employee in our store is dedicated to serving you, address: Haidian District, Beijing.

Leave a Comment