Python Forum
dynamic f-string example
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
dynamic f-string example
#4
The normal format-method does not allow function calls.
The object is still an str, still has the method upper, which
is also visible with dot-access in the string, but a call is not possible.
Before you send your values to the format method, you have to do all calls there or before.
silly = 'crazy'
exs = [
    "hellp {silly} world",
    "hey {silly}, this script is {silly_capital}!",
    "1 + 1 = {result} ... how {silly}!",
    ]
for ex in exs:
    result = ex.format(silly=silly, silly_capital=silly.upper(), result=2)
    print(result)
Or if all information are available:
a = 1
b = 1
x = a + b
silly = 'crazy'
exs = [
    f"hellp {silly} world",
    f"hey {silly}, this script is {silly.upper()}!",
    f"{a} + {b} = {x} ... how {silly}!",
    ]
for ex in exs:
    print(ex)
In the case you have to call this repeating, you can wrap it into a function:
def foo(a, b, silly):
    x = a + b
    exs = [
        f"hellp {silly} world",
        f"hey {silly}, this script is {silly.upper()}!",
        f"{a} + {b} = {x} ... how {silly}!",
        ]
    for ex in exs:
        print(ex)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
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