use-f-string¶
Prefer f-strings over percent formatting and str.format calls.
Message¶
Do not use printf style formatting or .format(). Use f-string instead to be more readable and efficient.
References¶
Settings¶
| Setting | Description | Type | Default |
|---|---|---|---|
| simple_expression_max_length | Maximum expression length to autofix inline in an f-string. | int | 30 |
Valid examples¶
somebody='you'; f"Hey, {somebody}."
"hey"
"hey" + "there"
b"a type %s" % var
u"plain unicode string"
Invalid examples¶
"Hey, {somebody}.".format(somebody="you")
"%s" % "hi"
Suggested fix
f"{'hi'!s}"
"a name: %s" % name
Suggested fix
f"a name: {name!s}"
Show more
u"%s" % name
Suggested fix
f"{name!s}"
"an attribute %s ." % obj.attr
Suggested fix
f"an attribute {obj.attr!s} ."
r"raw string value=%s" % val
Suggested fix
fr"raw string value={val!s}"
"{%s}" % val
Suggested fix
f"{{{val!s}}}"
"{%s" % val
Suggested fix
f"{{{val!s}"
"The type of var: %s" % type(var)
Suggested fix
f"The type of var: {type(var)!s}"
"%s" % obj.this_is_a_very_long_expression(parameter)["a_very_long_key"]
Options
simple_expression_max_length = 100
"%s" % abcdefghijklmnopqrstuvwxyz1234567890
Suggested fix
f"{abcdefghijklmnopqrstuvwxyz1234567890!s}"
"type of var: %s, value of var: %s" % (type(var), var)
Suggested fix
f"type of var: {type(var)!s}, value of var: {var!s}"
'%s" double quote is used' % var
Suggested fix
f'{var!s}" double quote is used'
"var1: %s, var2: %s, var3: %s, var4: %s" % (class_object.attribute, dict_lookup["some_key"], some_module.some_function(), var4)
Suggested fix
f"var1: {class_object.attribute!s}, var2: {dict_lookup['some_key']!s}, var3: {some_module.some_function()!s}, var4: {var4!s}"
"a list: %s" % " ".join(var)
Suggested fix
f"a list: {' '.join(var)!s}"
"%s" % (first, second)