NOTE: No feature change is planned for this extension. The inbuilt settings in vscode could be an alternative to explore.
This extension converts python string to f-string and vice-versa based on presence of {.*} within quotes or not respectively.
Since f-strings feature is available python 3.6 onwards, this extension should be enabled with python versions 3.6+ only. This does not play well with str.format().
This extension is inspired by https://github.com/meganrogge/template-string-converter which helps with backquotes very nicely.
This extension converts a string to f-string if {.*} is found within quotes by prepending f to the starting quote position. This evaluation is triggered on each change to code.
Similarly if the above pattern is not found for a string used as f-string, it remove the f before starting quote position.
Even if the string is going to be a f-string, starting with f will remove until the desired pattern is encountered. So, with this extension we need not prepend f to any string at all.
At any time undo in editor will revert the genrated code edits.
Conversion Example:
| Input | Output |
|---|---|
"" |
"" |
f"" |
"" |
"{}" |
"{}" |
f"{}" |
"{}" |
"{a}" |
f"{a}" |
f"{a}" |
f"{a}" |
"{a}" + "{b}" + "c" |
f"{a}" + f"{b}" + "c" |
- This extension should only be enabled when
pythonversion3.6+is in use.
This extension contributes the following settings:
fstring-converter.enable: enable/disable this extension
- If used with
pythonversions below3.6, code will be altered but the resulting code will generate syntax errors. This extension currently does not detect python version and can be disabled manually, if needed. - This extension does not work with
str.format()either. However withpython3.6+version we really do not needstr.format()and all its uses can be achieved withf-strings. - This does not look at generating code with valid syntax, rather just add or remove
fas needed. - Multi line strings or strings quoted with
"""or'''in a single line are currently not supported.f-stringsthat span multiple line may be written as below which can be handled correctly.
my_str = (
f"sample {f_string} "
"continues with a simple string "
f"terminates with another {f_string}"
)
