0

I've read PEP498 and multiple articles, forums, etc and finally reached a point when I am asking you ppl for help. Yes, I accept a possibility that maybe I simply did not understand what I read :) so would appreciate that pointing out too ;P

First the code then my use case. I want to do this but with f strings:

input = 'this is {some} input.'
some = 'test'
print(input.format(some=some))

> this is test input.

So I have a string variable with a "{some}" variable to later be changed by str.format() How can I do it with f strings? I know I can pass a string variable into an f string but how can I affect the {some} variable within that string variable itself?

input = 'this is {some} input.'
some = 'test'
print(f'{input}')

> this is {some} input.

USE CASE:

What I am trying to do is to read a file containing a sql query with parameters. I need to be able to change the tables in the FROM section of the query. these cannot be sql parameters as its not supported. Hence I am reading the file and want it to contain a "{table}" variable placeholder which I can then format when reading in the file. I am able to do it with the .format() method but I am curious if anyone can answer this using f strings.

Krystian
  • 1
  • 1
  • Why do you need an f-string? What's wrong with `print(input.format(**locals()))`? It will not work with an f-string. – pts Jan 03 '20 at 11:43
  • See https://stackoverflow.com/questions/44757222/transform-string-to-f-string or https://stackoverflow.com/questions/47339121/how-do-i-convert-a-string-into-an-f-string – Thierry Lathuille Jan 03 '20 at 11:44
  • simply wanted to know if there is a way to do in with f strings so I can stop thinking what am I missing :) – Krystian Jan 03 '20 at 11:48

0 Answers0