question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

convert f-string with nothing to interpolate to regular string

See original GitHub issue

Is your feature request related to a problem? Please describe.

f-strings are great. sometimes they are so great we might use them even when we don’t need to, such as in files where an f-string is used but nothing to interpolate was included in the string body.

Describe the solution you’d like

say you had a python script with a line like this:

hello = f"hello"

black should see that f-string, recognize that it could just be a regular string and drop the f-prefix.

output:

hello = "hello"

Describe alternatives you’ve considered

Could return an error message, but I want black to format for me. Could also not do this, but I think changing to a regular string is a better option.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:15
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
dougthor42commented, Jun 5, 2022

Half the time I forget to add the f prefix. The other half the time I forget to add the curly brackets around my var. I think automatically dropping a “looks useless to me” f prefix is a bad idea. How can black know what the user intended?

print(f"myvar=")
print("{myvar=}")

The same argument applies to the "automatically add f prefix.

Sorry, but 👎 for me.

1reaction
vemelcommented, Nov 19, 2019

This can potentially be dangerous for f-strings with brackets.

s = f"this can be safely transformed to a regular string"
s2 = f"Here we have {{double_brackets}}, should they be replaced with single? Who knows..."
Read more comments on GitHub >

github_iconTop Results From Across the Web

string interpolation - format string output - Microsoft Learn
To identify a string literal as an interpolated string, prepend it with the $ symbol. You can't have any white space between the...
Read more >
how I convert string into interpolated string? - Stack Overflow
Find & replace can stil be used with a regex. You could use an object instead of seperate variables with the data you...
Read more >
Python String Interpolation - Programiz
String interpolation is a process substituting values of variables into placeholders in a string. For instance, if you have a template for saying...
Read more >
Python String Interpolation - Towards Data Science
String interpolation is a process of injecting value into a placeholder (a placeholder is nothing but a variable to which you can assign ......
Read more >
Python String Formatting Best Practices
Learn the four main approaches to string formatting in Python, ... #3 String Interpolation / f-Strings (Python 3.6+); #4 Template Strings (Standard Library) ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found