1

00:00:00,840  -->  00:00:05,550
Welcome to lesson 21 in this short lesson we'll talk about string formatting.

2

00:00:05,550  -->  00:00:10,320
Normally when you want to combine strings together to form a single string value you can use the plus

3

00:00:10,320  -->  00:00:13,030
operator to concatenate strings together.

4

00:00:13,380  -->  00:00:21,490
For example you can have a blue plus the string world and that evaluates to the single string value

5

00:00:21,630  -->  00:00:23,060
Hello world.

6

00:00:23,130  -->  00:00:28,770
This can get tricky if you have several strings that you need to concatenate create several variables

7

00:00:28,790  -->  00:00:28,910
.

8

00:00:29,010  -->  00:00:38,710
Let's have name equals the string Alice This place is Main Street Time is the string 18:00.

9

00:00:38,940  -->  00:00:41,870
Food is the string turnips.

10

00:00:42,210  -->  00:00:47,520
And then we have a large string that we want to concatenate all of these together with it look like

11

00:00:48,060  -->  00:01:00,750
a simple invitation so we can have hello plus name plus you are invited to a party hat and then we'll

12

00:01:00,750  -->  00:01:10,260
concatenate the place variable then add at plus time plus period.

13

00:01:10,260  -->  00:01:12,860
Please bring.

14

00:01:13,350  -->  00:01:19,850
Then plus food and then plus for that final period that evaluates to.

15

00:01:19,860  -->  00:01:20,820
Hello Alice.

16

00:01:20,820  -->  00:01:23,900
Alice you are invited to a party at Main Street at 6pm.

17

00:01:23,940  -->  00:01:25,440
Please bring turnips.

18

00:01:25,440  -->  00:01:27,310
Sounds like a great party huh.

19

00:01:27,690  -->  00:01:33,680
Anyway this expression right here looks really complicated we had to take a lot of quotes allow pluss

20

00:01:33,680  -->  00:01:34,370
.

21

00:01:34,470  -->  00:01:38,940
It's kind of hard to read as well so pile on has something called string formatting.

22

00:01:38,940  -->  00:01:40,820
It's also called String interpellation.

23

00:01:40,980  -->  00:01:48,270
And this lets us put a percent s inside of a string to mark where we want to have other strings inserted

24

00:01:48,270  -->  00:01:49,160
into it.

25

00:01:49,350  -->  00:01:50,440
I'll show you what I mean.

26

00:01:50,730  -->  00:01:57,630
So we can have this exact same expression typed out like this hello and then instead of this end quote

27

00:01:57,660  -->  00:02:03,810
plus name plus and then another quote We can just have percent S and we can type out the rest of the

28

00:02:03,810  -->  00:02:04,360
string.

29

00:02:04,550  -->  00:02:09,460
You are invited to a party that percent s percent.

30

00:02:09,520  -->  00:02:12,410
Yes please bring.

31

00:02:12,610  -->  00:02:13,090
Percent.

32

00:02:13,120  -->  00:02:13,770
Yes.

33

00:02:13,900  -->  00:02:15,140
And then period.

34

00:02:15,600  -->  00:02:21,750
And then after that string we can have a percent sign followed by parentheses.

35

00:02:21,780  -->  00:02:26,850
And inside the parentheses will have a comma delimited list of variables that we want to have inserted

36

00:02:27,150  -->  00:02:28,580
at these percent S's.

37

00:02:28,650  -->  00:02:31,730
These percent S's are called conversion specifiers.

38

00:02:31,770  -->  00:02:41,100
So you can have the string in name be assigned or be inserted at the first percent S and then we can

39

00:02:41,320  -->  00:02:45,050
place time and food.

40

00:02:45,570  -->  00:02:51,390
And this evaluates to the exact same thing that this huge string concatenation expression evaluate to

41

00:02:51,560  -->  00:02:55,090
Helliwell as you're invited to a party at Main Street at 6.

42

00:02:55,110  -->  00:02:56,670
Please bring turnips.

43

00:02:56,850  -->  00:03:03,570
Now this string is a lot easier to read and we can put all of the strings that we want to insert side

44

00:03:03,570  -->  00:03:04,740
of it at the end.

45

00:03:04,740  -->  00:03:05,260
After this.

46

00:03:05,300  -->  00:03:11,940
Percent and then have it inside parentheses by using string formatting you can make your code a lot

47

00:03:11,940  -->  00:03:12,870
more readable.
