+ 1
What is f string and how it's work ?
2 Answers
+ 2
F-strings provide a way to embed expressions inside string literals, using a minimal syntax. It should be noted that an f-string is really an expression evaluated at run time, not a constant value. In Python source code, an f-string is a literal string, prefixed with 'f', which contains expressions inside braces.
simple example :
https://code.sololearn.com/cWWRRmnNnlQq/?ref=app
https://www.python.org/dev/peps/pep-0498/#:~:text=F-strings%20provide%20a%20way,which%20contains%20expressions%20inside%20braces.
0
f string Can be used to comfortably Format strings.
For example
f"this string contains the variable {var} "
This will be formatted to
"this string contains the variable 123.0"
if var is assigned the value 123.0 (var = 123.0)
The Format is pretty intuitive. f strings Start with an f, followed by Single or double quotes, surrounding the string.
Variables or expression are sourounded by curly braces and will be resolved in the resulting string.
You could also give Operations like
f"1 + 2 = {1+2}" which will be formatted to" 1 + 2 = 3"
Or for sure
a = 1
b = 2
print(f"{a} + {b} = {a+b}")
Which will print "1 + 2 = 3".
Hope this helps