Python Forum
f-string capability in a function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
f-string capability in a function
#8
(Dec-25-2019, 02:33 AM)Skaperen Wrote: could you show a complete and simple example?
Here one with in comparison with f-string.
from jinja2 import Template

name = 'Kent'
age = 34
f_string = f"My name is {name} and I am {age}"
j_string = "My name is {{ name }} and I am {{ age }}"
tm = Template(j_string)
jinja_msg = tm.render(name=name, age=age)
print(f_string)
print(jinja_msg)
Output:
My name is Kent and I am 34 My name is Kent and I am 34
Here using {{ }} - expressions to print to the template output.

{% %} - statements,like if,elif,else,for...ect.
from jinja2 import Template

t = Template("Numbers are: {% for n in range(1,10) %}{{n}} " "{% endfor %}")
print(t.render())
Output:
Numbers are: 1 2 3 4 5 6 7 8
>>> from jinja2 import Template
>>> 
>>> Template("{{ 10 ** 3 }}").render()
'1000'
>>> from jinja2 import Template
>>>
>>> def foo():
...    return "foo() called"
...
>>>
>>> Template("{{ foo() }}").render(foo=foo)
'foo() called'
Reply


Messages In This Thread
f-string capability in a function - by Skaperen - Dec-24-2019, 06:10 AM
RE: f-string capability in a function - by ibreeden - Dec-24-2019, 12:46 PM
RE: f-string capability in a function - by Skaperen - Dec-24-2019, 08:52 PM
RE: f-string capability in a function - by DeaD_EyE - Dec-24-2019, 03:31 PM
RE: f-string capability in a function - by snippsat - Dec-24-2019, 08:31 PM
RE: f-string capability in a function - by snippsat - Dec-24-2019, 09:14 PM
RE: f-string capability in a function - by Skaperen - Dec-25-2019, 02:33 AM
RE: f-string capability in a function - by snippsat - Dec-25-2019, 09:22 PM
RE: f-string capability in a function - by Skaperen - Dec-26-2019, 03:19 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  passing a string to a function to be a f-string Skaperen 3 4,158 Nov-06-2020, 06:14 AM
Last Post: Gribouillis
  a function to look for dates in a string Skaperen 6 4,135 May-09-2020, 11:03 PM
Last Post: Skaperen
  function t split a string Skaperen 2 1,909 Apr-23-2019, 06:03 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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