On this page
string.Formatter
Prev: string.capwords | Up: string | Next: string.Template
Rel: string.capwords, string.Template
Ref: docs.python.org — string.Formatter
Tags: #public #python3 #stdlib #string
Import
import string
from string import Formatter
Signature & Overview
string.Formatter()
string.Formatter class.
Methods & Attributes
Formatter.check_unused_args(self, used_args, args, kwargs)—Formatter.check_unused_argsmethod/property.Formatter.convert_field(self, value, conversion)—Formatter.convert_fieldmethod/property.Formatter.format(self, format_string, /, *args, **kwargs)—Formatter.formatmethod/property.Formatter.format_field(self, value, format_spec)—Formatter.format_fieldmethod/property.Formatter.get_field(self, field_name, args, kwargs)—Formatter.get_fieldmethod/property.Formatter.get_value(self, key, args, kwargs)—Formatter.get_valuemethod/property.Formatter.parse(self, format_string)—Formatter.parsemethod/property.Formatter.vformat(self, format_string, args, kwargs)—Formatter.vformatmethod/property.
Usage
import string
class DefaultFormatter(string.Formatter):
def get_value(self, key, args, kwargs):
return kwargs.get(key, f'<{key}>')
assert DefaultFormatter().format('Hi {name} ({role})', name='Ada') == 'Hi Ada (<role>)'
Gotchas
- Subclass
Formatterto customize{field}lookup or formatting rules cleanly.
REPL Inspect
from string import Formatter
help(Formatter)
Linked from
Related notes
- tuts/python3/stdlib_module/string/capwords Linked here; links to this page; shared tags: #stdlib, #string
- tuts/python3/stdlib_module/string/Template Linked here; links to this page; shared tags: #stdlib, #string
- tuts/python3/stdlib_module/string Linked here; links to this page; shared tags: #stdlib
- tuts/c/stdlib/string shared tags: #stdlib, #string
- tuts/c/stdlib/string/memchr shared tags: #stdlib, #string
- tuts/c/stdlib/string/memcmp shared tags: #stdlib, #string
- tuts/c/stdlib/string/memcpy shared tags: #stdlib, #string
- tuts/c/stdlib/string/memmove shared tags: #stdlib, #string
- tuts/c/stdlib/string/memset shared tags: #stdlib, #string
- tuts/c/stdlib/string/strcat shared tags: #stdlib, #string