1
00:00:04,440 --> 00:00:09,730
Python 3.6 introduced another way of
formatting values in a string.

2
00:00:09,730 --> 00:00:13,809
These are called f-strings or formatted string literals. Now at the moment I won't be

3
00:00:13,809 --> 00:00:18,130
using f-strings in the course. That's
because there's no plans to back port

4
00:00:18,130 --> 00:00:23,480
them to earlier versions of Python 3.
Python 3.4 is still very common,

5
00:00:23,480 --> 00:00:28,179
and f-strings won't work with that version, or
earlier versions. But with that said,

6
00:00:28,179 --> 00:00:32,470
it is worth being familiar with them. So let's
have a quick look. I'm going to use the

7
00:00:32,470 --> 00:00:36,500
strings.py file from an earlier
video, so I'm going to select that,

8
00:00:36,500 --> 00:00:41,420
and we're going to work with this file. Now at the moment, we've got an error on the last line.

9
00:00:41,420 --> 00:00:45,160
I used that to demonstrate that you
can't concatenate a string and an int.

10
00:00:45,160 --> 00:00:49,380
So let's firstly just check this by running
it again, confirming we've still got the error.

11
00:00:49,380 --> 00:00:52,500
And you can see we've still got this type
error "can only concatenate strings,

12
00:00:52,500 --> 00:00:55,200
not an int to string".

13
00:00:55,200 --> 00:00:59,199
So let's fix that using
an f-string. So all we really need to do

14
00:00:59,199 --> 00:01:04,720
to fix that, is to put the letter f
before is up here. Put an f before the

15
00:01:04,720 --> 00:01:08,710
double quote and that's literally all we
need to do. So an f-string is defined

16
00:01:08,710 --> 00:01:12,490
by putting the letter f before the
opening quotes. Now that we've done that,

17
00:01:12,490 --> 00:01:17,500
we can use a variable name, inside curly
braces, in a similar way that we've used

18
00:01:17,500 --> 00:01:23,560
replacement fields. So what we can do is
delete this extra double quote here,

19
00:01:23,560 --> 00:01:30,220
come up and put age there. We can put age in
left and right curly braces, like so.

20
00:01:30,220 --> 00:01:35,100
We can remove the other double quote there,
so we've got it all in the one string, now

21
00:01:35,100 --> 00:01:38,980
So as you can see, it looks very
similar to a replacement field. It's a lot

22
00:01:38,980 --> 00:01:43,240
cleaner, because we don't have to use
.format at the end anymore. So if we run

23
00:01:43,260 --> 00:01:47,680
the program, let's do that just to make
sure that it works, and you can see we've no

24
00:01:47,680 --> 00:01:52,420
longer got an error. The output is
showing Tim is 24 years old - and Tim

25
00:01:52,420 --> 00:01:57,010
wishes he was 24 years old. Alright, so
all the formatting for replacement

26
00:01:57,010 --> 00:02:02,890
fields also works with f-strings. So, for
example, I could do some formatting

27
00:02:02,890 --> 00:02:08,440
along these lines. So let's just type
some code in. So on line 24 I'm going to type

28
00:02:08,440 --> 00:02:16,920
print parentheses f double quotes Pi is
approximately,

29
00:02:16,920 --> 00:02:19,960
and then in left and right curly braces,
noting that the right one's added for us

30
00:02:19,960 --> 00:02:29,980
automatically, I can type in an
expression 22 / 7:12.50f, and then we've got

31
00:02:29,980 --> 00:02:34,420
our double quote and then our right
parenthesis. Now clearly you can see that

32
00:02:34,420 --> 00:02:38,320
I've used an expression rather than a
variable name there. So let's just try

33
00:02:38,320 --> 00:02:44,060
running that first, and you can see we've
got the value showing.

34
00:02:44,060 --> 00:02:49,140
Now what we can also do is calculate 22 / 7 first, and then use a variable,

35
00:02:49,150 --> 00:02:53,709
so let's do that.
So PI on line 25 is equal to 22 divided

36
00:02:53,709 --> 00:03:04,690
by 7. Line 23, we'll do a print parenthesis f
double quotes Pi is approximately

37
00:03:04,690 --> 00:03:11,020
and left and right curly braces, and this
time we'll use the variable, so pi:12.50f

38
00:03:11,020 --> 00:03:13,540
for formatting,

39
00:03:13,540 --> 00:03:16,300
and, sorry for precision,  and then we'll run that again,

40
00:03:16,300 --> 00:03:19,260
and you can see we've got the
identical output. So now that you

41
00:03:19,269 --> 00:03:23,560
understand replacement fields, you can
change to using f-strings instead, if you

42
00:03:23,560 --> 00:03:29,240
want. But be aware that your code won't
run on Python 3 versions, earlier than 3.6,

43
00:03:29,240 --> 00:03:33,100
if you do use them. In the next video,
we'll finish this section with a quick

44
00:03:33,100 --> 00:03:35,800
look at yet another way to format strings.

45
00:03:35,800 --> 00:03:38,800
See you in the next video.

