Fundamentals 8 min read

Python String Formatting: Percent (%) and format() Methods with Detailed Examples

This article explains Python's two main string‑formatting techniques—the legacy percent (%) operator and the newer format() method—detailing their syntax, flags, width, precision, type codes, and providing numerous code examples with expected output.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Python String Formatting: Percent (%) and format() Methods with Detailed Examples

The article begins by introducing the percent (%) formatting style, showing the general syntax %[(name)][flags][width].[precision]typecode and describing each component such as name, flags (e.g., +, -, space, 0), width, precision, and typecode (s, r, c, d, o, x, X, e, E, f, F, g, G, %).

It then lists the meaning of each flag and typecode, explaining how to control alignment, sign display, padding, numeric bases, scientific notation, and percentage output. Sample percent‑format strings are provided, for example:

s1 = "i am %s, i am %d years old" % ('jeck', 26)
s2 = "i am %(name)s, i am %(age)d years old" % {'name':'jeck','age':26}
s3 = "i am %(name)+10s, i am %(age)d years old, i am %(height).2f" % {'name':'jeck','age':26,'height':1.7512}
s4 = "原数: %d, 八进制:%o , 十六进制:%x" % (15,15,15)
s5 = "原数:%d, 科学计数法e:%e, 科学计数法E:%E" % (1000000000,1000000000,1000000000)
s6 = "百分比显示:%.2f %%" % 0.75

Running the above snippets produces output such as:

i am jeck, i am 26 years old
i am     jeck, i am 26 years old, i am 1.75
原数:15, 八进制:17, 十六进制:f
原数:1000000000, 科学计数法e:1.000000e+09, 科学计数法E:1.000000E+09
百分比显示:0.75, 75.84%

The second part covers the newer format() method. It outlines the format string syntax {[fill][align][sign][#][0][width][,][.precision][type]} and explains each option, including fill characters, alignment (<, >, =), sign handling, alternate form (#), zero‑padding, width, thousands separator, precision, and type specifiers (s, d, b, o, x, X, e, E, f, F, g, G, %).

Example format() snippets are shown:

f1 = "i am {0}, i am {1}d years old".format('Jeck', 26)
f2 = "i am {name}, i am {age}d years old".format(**{'name':'jeck','age':26})
f3 = "--{name:*^10s}--   =={age:<10.2f}==".format(name='Jeck', age=26.457)
f4 = "原数:{:d}  二进制:{:b}, 八进制:{:o}, 十六进制x:{:x},十六进制X:{:X}".format(15,15,15,15,15)
f5 = "原数:{:d}, 科学计数法e:{:e}, 科学计数法E:{:E}".format(1000000000,1000000000,1000000000)
f6 = "原数:{:2F}, 百分号表示{:.2%},  原数:{:d},自动分割表示:{:,}".format(0.75,0.7584,10000000,10000000)

Printing these variables yields results such as:

i am Jeck, i am 26d years old
--***Jeck***--==26.46==
原数:15二进制:1111,八进制:17,十六进制x:f,十六进制X:F
原数:1000000000,科学计数法e:1.000000e+09,科学计数法E:1.000000E+09
原数:0.750000,百分号表示75.84%,原数:10000000,自动分割表示:10,000,000

Overall, the article serves as a concise reference for Python developers to master both legacy and modern string‑formatting techniques.

Fundamentalsexamplespercent operatorString Formattingformat method
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.