% là string formatting operator (interpolation operator). Cho format % values (format là string), % conversion specifications trong format được thay bằng zero hoặc nhiều element của values. Tương tự sprintf() trong C. Ví dụ:
>>> lang = "Python" >>> print "%s is awesome!" % lang Python is awesome
Cũng format số với ký pháp này. Ví dụ:
>>> cost = 128.527 >>> print "The book costs $%.2f at the bookstore" % cost The book costs $128.53 at the bookstore
Cũng dùng dictionary interpolate string. Cú pháp: cung cấp key trong ngoặc đơn giữa % và conversion character. Ví dụ:
print('%(language)s has %(number)03d quote types.' % {'language': "Python", "number": 2})
Python has 002 quote types.
Đọc thêm về string formatting: https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting