Python Forum
dynamic f-string example
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
dynamic f-string example
#1
you might want to put this script in your python examples folder. you won't likely ever need to do this, but if some day you need to dynamically change the format of an f-string at run time (normally, python constructs code from an f-string at compile time when it reads in the source file), this can be a great reminder example of how to set it up.
silly = 'crazy'
exs = ['hellp {silly} world',
"""hey {silly}, this script is {silly.upper()}!""",
"1 + 1 = {1+1} ... how {silly}!"]
for ex in exs:
    result = eval(f'f{ex!r}')
    print(result)
always use a trusted source of all characters for an f-string format. using untrusted sources like user input over the internet can get your script exploited by the black hats.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Messages In This Thread
dynamic f-string example - by Skaperen - Feb-16-2020, 03:53 AM
RE: dynamic f-string example - by DeaD_EyE - Feb-16-2020, 12:09 PM
RE: dynamic f-string example - by Skaperen - Feb-17-2020, 12:09 AM
RE: dynamic f-string example - by DeaD_EyE - Feb-17-2020, 07:08 AM
RE: dynamic f-string example - by Skaperen - Feb-18-2020, 12:51 AM
RE: dynamic f-string example - by DeaD_EyE - Feb-18-2020, 09:31 AM
RE: dynamic f-string example - by Skaperen - Feb-18-2020, 06:49 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020