format() Format a value

Last updated: 2026-09-22

format() Format a value

format(value, format_spec='') returns value formatted according to format_spec.

CategoryBuilt-in Functions
KindBuilt-in Function
Python Versionall

📝 Syntax

format(value[, format_spec])

⚙️ Parameters

value RequiredThe value to format.
format_spec OptionalFormat specifier, e.g. '.2f', '08b'.(Default:'')

Returns:str. The value formatted according to format_spec.

💥 Raises

▶ Example

Edit the code, press Run, and see the result in the output panel:

Output:

3.14
11111111
0xff
More case studies coming soon — check back later.

❓ FAQ

QIs format() the same syntax as the colon in f-strings?
AYes — f'{3.14159:.2f}' internally calls format(3.14159, '.2f'); the two are fully equivalent and interchangeable.
QWhat does format(5, '08b') return?
AIt returns '00000101': 'b' means binary, '8' means a width of 8 digits, and '0' means zero-pad on the left.
QWhat does format(255, '#x') return?
AIt returns '0xff'; the '#' flag makes hexadecimal include the 0x prefix, without it you get 'ff'. Likewise '#o' and '#b' add the 0o and 0b prefixes.
QWhat happens when the format specifier is wrong?
AIt raises a ValueError or TypeError (e.g. format(1.5, 'abc')) — the specifier syntax and per-type support are strictly validated.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏