Hi there Serdar Yegulalp here from Infoworld
at IDG. Today i'll be looking at f strings in python this is the feature
that lets you format data for printing using string templates
now f strings are relatively new python added them with version 3.6 so
for a while many tutorials didn't discuss them and
only recently have python packages including items in the standard library
started using them more often but at this point they're the best first choice
for how to format data because they elegantly cover the vast
majority of use cases let's take a look in this first example
here of how an f-string works it's simply a string prefixed with f and
with any variables you want inserted into the string
delineated with curly braces in this first example we have two variables the
string name and the date value last sign on when we
print that string those two variables are automatically
converted to their string representations and inserted into the
string at those points note that f strings can only work if the
variables referenced in the string are actually in the name space at that time
the f prefix on the string causes the binding of variables into it to take
place right when it's declared so if you want to create a string
beforehand and bind variables into it later
then the older format function will do that for you and as you can see
the string format used by f strings is basically the same string format used by
the format function so if you've already been using format
you'll have some idea of how to use f strings too
also as an aside this situation i've described right here is a good test for
whether to use f strings or format if you have a string
template that you want to define ahead of time and reuse in multiple contexts
use format with a regular string if you're formatting for output right away
which is the more common scenario use an f string and you can mix them up
too you can use f strings to generate strings used for variables in a format
function or vice versa now before f strings came along python
had a mini formatting language that was used to format how things like dates and
floating point numbers were printed fstrings let us use that formatting too
in this example for formatting dates we can take
our date value and print it as a month in a year by
using a colon to separate the name of the variable from its formatting
information the same thing goes for formatting a decimal value in this
example another convenient feature in f strings
if you use an exclamation point after the variable you can then use s r
or a to obtain the string conversion the representation or the ascii
converted representation of an object respectively
python38 added an f string feature that's very useful for debugging if you
place an equal sign after a variable expression in an
f-string the name of the variable and its value
are inserted into the string at that point rather than just the value
if you've worked at all with strings you ought to know by now that you can escape
quotes within strings a number of different ways
one common way is to use different quote types for the string definition versus
the contents of the string with f strings you have to use different
quotes within f string variable expressions
than you do for the string definition itself in this example i'm using double
quotes for the f string and single quotes for the inner
expression another way to get around this is just
to create temporary variable bindings and use those instead so you're not
fussing with strings inside strings and potentially making more headache for
yourself another pitfall to watch out for when
using a string in an f-string variable expression
is backslash escapes you can't use them inside a string that's in an f-string
variable because otherwise the interpreter has no
idea where the escaping is taking place whether it's in the f-string itself or
in the f-string variable reference if we uncomment this code our script
won't even run now because python can't parse that line
again to get around this just use a temporary variable binding
it'll probably make your code more readable overall anyway
if you like this video please leave a comment below don't forget to subscribe
to the idg tech talk channel on youtube and for more python tips be sure to
follow us on facebook youtube and
infoworld.com