Skip to content

Remove Str From Fstring

Documentation Index

Fetch the complete documentation index at: https://docs.sourcery.ai/llms.txt

Use this file to discover all available pages before exploring further.

Sourcery refactoring id: remove-str-from-fstring

Section titled “Sourcery refactoring id: remove-str-from-fstring”

Remove unnecessary calls to str() within formatted values in f-strings

def description(name: str, age: int):
return f"Name: {str(name)}, Age: {str(age)}"
def description(name: str, age: int):
return f"Name: {name}, Age: {age}"

Calls to str() are normally unnecessary within the formatted values of f-strings, since this conversion is already implicitly made. Removing them tidies up the code slightly and will yield a small performance improvement. You do need to call str() on some types with custom __repr__() methods, so this refactoring only triggers where Sourcery has inferred the type to be suitable.