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
Invalid examples¶
"Hey, {somebody}.".format(somebody="you")
"%s" % "hi"
Suggested fix
f"{'hi'}"
"a name: %s" % name
Suggested fix
f"a name: {name}"
Show more
"an attribute %s ." % obj.attr
Suggested fix
f"an attribute {obj.attr} ."
r"raw string value=%s" % val
Suggested fix
fr"raw string value={val}"
"{%s}" % val
Suggested fix
f"{{{val}}}"
"{%s" % val
Suggested fix
f"{{{val}"
"The type of var: %s" % type(var)
Suggested fix
f"The type of var: {type(var)}"
"%s" % obj.this_is_a_very_long_expression(parameter)["a_very_long_key"]
"%s" % abcdefghijklmnopqrstuvwxyz1234567890
Suggested fix
f"{abcdefghijklmnopqrstuvwxyz1234567890}"
"type of var: %s, value of var: %s" % (type(var), var)
Suggested fix
f"type of var: {type(var)}, value of var: {var}"
'%s" double quote is used' % var
Suggested fix
f'{var}" 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}, var2: {dict_lookup['some_key']}, var3: {some_module.some_function()}, var4: {var4}"
"a list: %s" % " ".join(var)
Suggested fix
f"a list: {' '.join(var)}"