1
00:00:00,000 --> 00:00:02,990
The string format method is very powerful and useful tool.

2
00:00:03,000 --> 00:00:05,990
You'll see it used a lot and you will use it a lot.

3
00:00:06,000 --> 00:00:07,990
It looks like this.

4
00:00:08,000 --> 00:00:11,990
Let's start by assigning a couple of variables a and b = 5 and 42.

5
00:00:12,000 --> 00:00:14,990
And then we'll print them out.

6
00:00:15,000 --> 00:00:19,990
If I were to just say print a, b like that then we get the numbers.

7
00:00:20,000 --> 00:00:22,990
But that's not terribly useful or descriptive.

8
00:00:23,000 --> 00:00:25,990
And you might want to look at it more like this.

9
00:00:26,000 --> 00:00:31,990
You might want to say this is and put in a placeholder and that is and put in

10
00:00:32,000 --> 00:00:32,990
another placeholder.

11
00:00:33,000 --> 00:00:38,990
And then use the format method and say a and b. And now you get a string,

12
00:00:39,000 --> 00:00:41,990
'this is 5 and that is 42.'

13
00:00:42,000 --> 00:00:43,990
So this is what the format method does.

14
00:00:44,000 --> 00:00:46,990
It allows you to put placeholders in a string and then replace those

15
00:00:47,000 --> 00:00:50,990
placeholders with values and return a new string.

16
00:00:51,000 --> 00:00:55,990
It's important to note that the string that's returned is a new string.

17
00:00:56,000 --> 00:00:56,990
Strings are immutable in Python.

18
00:00:57,000 --> 00:01:01,990
So this string is an entirely different string than that string.

19
00:01:02,000 --> 00:01:06,990
So if I had a string that just had this in it, do a copy and paste here, and say

20
00:01:07,000 --> 00:01:09,990
s = and put that in it.

21
00:01:10,000 --> 00:01:10,990
Now I have that string.

22
00:01:11,000 --> 00:01:12,990
If I just look at s, that's what's in it.

23
00:01:13,000 --> 00:01:18,990
If I say s.format(a, b), I get a new string.

24
00:01:19,000 --> 00:01:21,990
So the id of s is this.

25
00:01:22,000 --> 00:01:32,990
And if I say new = s.format(a, b) and look at the id of that, I have a different id.

26
00:01:33,000 --> 00:01:35,990
Those are two entirely different strings.

27
00:01:36,000 --> 00:01:38,990
So format returns a new string.

28
00:01:39,000 --> 00:01:43,990
It's also important to note that format is new as of Python 3.

29
00:01:44,000 --> 00:01:46,990
Format was not available in Python 2.

30
00:01:47,000 --> 00:01:48,990
In Python 2 things were done differently.

31
00:01:49,000 --> 00:01:51,990
In Python 2 you might have done some thing like this.

32
00:01:52,000 --> 00:02:00,990
this is %d, that %d. And then use a % operator and have a and b.

33
00:02:01,000 --> 00:02:06,990
So this was overloading the % operator and it was using C style format strings.

34
00:02:07,000 --> 00:02:09,990
And we just felt that this is obsolescent, and it needed to be replaced with

35
00:02:10,000 --> 00:02:10,990
something more powerful.

36
00:02:11,000 --> 00:02:13,990
And that's what happened in Python 3.

37
00:02:14,000 --> 00:02:15,990
The format method is more powerful.

38
00:02:16,000 --> 00:02:19,990
It's a more consistent syntax and it's actually a lot easier to use.

39
00:02:20,000 --> 00:02:24,990
One thing in particular that you can note here is that using this old way you

40
00:02:25,000 --> 00:02:25,990
needed to know a type.

41
00:02:26,000 --> 00:02:28,990
This %d represents a decimal type.

42
00:02:29,000 --> 00:02:36,990
And using this new way it uses the repr method, repr, to look at the value and

43
00:02:37,000 --> 00:02:39,990
decide what type it is and print it out based on its type.

44
00:02:40,000 --> 00:02:44,990
So you don't need to know what type the variables are in order to use format.

45
00:02:45,000 --> 00:02:48,990
Another thing worth noting is that this method that we are using here, and this

46
00:02:49,000 --> 00:02:52,990
gets used a lot in this course and you will see it a lot in new code with the

47
00:02:53,000 --> 00:02:56,990
bare curly braces, is actually new in Python 3.1.

48
00:02:57,000 --> 00:03:02,990
In Python 3.0, you needed some sort of a indicator inside of the curly braces,

49
00:03:03,000 --> 00:03:05,990
a positional indicator or a name indicator.

50
00:03:06,000 --> 00:03:07,990
And we'll look at how those work in a moment.

51
00:03:08,000 --> 00:03:11,990
But in Python 3.1 and later, you don't need anything inside of those curly

52
00:03:12,000 --> 00:03:15,990
braces, and it simply takes the positional arguments based on their order.

53
00:03:16,000 --> 00:03:20,990
So if I wanted to print a and b in the opposite order, I would actually have to

54
00:03:21,000 --> 00:03:22,990
put them in the opposite order in the format.

55
00:03:23,000 --> 00:03:31,990
I'd have to change them here, and say b, a and then I will get 42 and 5.

56
00:03:32,000 --> 00:03:39,990
On the other hand if I wanted to, I could say 1 here and 0 here, and not change

57
00:03:40,000 --> 00:03:42,990
their order and it will print them in the opposite order.

58
00:03:43,000 --> 00:03:52,990
So you can specify positional arguments inside the curly braces that will allow you, [00:03:49.1] for example if you wanted to print the same value several times, you could

59
00:03:53,000 --> 00:04:03,990
say, this is 1 and that is {0} and this too is {1}.format(a, b).

60
00:04:04,000 --> 00:04:07,990
And now I am actually getting this 1 value twice and the 0 value once.

61
00:04:08,000 --> 00:04:11,990
So these numbers represent the positional order in which the arguments

62
00:04:12,000 --> 00:04:13,990
are provided to format.

63
00:04:14,000 --> 00:04:15,990
You can also use names.

64
00:04:16,000 --> 00:04:22,990
You can say this is {bob} and that is {fred}.

65
00:04:23,000 --> 00:04:32,990
And then name your arguments in format. bob = a, fred = b and that can be useful.

66
00:04:33,000 --> 00:04:41,990
In fact you could put them in a dictionary d= dictionary, bob = a and fred = b

67
00:04:42,000 --> 00:04:43,990
and then use that dictionary.

68
00:04:44,000 --> 00:04:50,990
'this is {bob} and that is {fred}'.format and you use this double asterisk to

69
00:04:51,000 --> 00:04:52,990
refer to the dictionary.

70
00:04:53,000 --> 00:04:54,990
So format is very powerful in this way.

71
00:04:55,000 --> 00:04:58,990
And in fact, it's an extremely rich language all on its own.

72
00:04:59,000 --> 00:05:02,990
The full documentation for format is available on the Python

73
00:05:03,000 --> 00:05:03,990
documentation website.

74
00:05:04,000 --> 00:05:09,990
And it has all kinds of ways that data can be formatted and ways that you can specify type.

75
00:05:10,000 --> 00:05:11,990
It's an extremely rich language in its own.

76
00:05:12,000 --> 00:05:14,990
And it's actually more powerful and more flexible than the old way of

77
00:05:15,000 --> 00:05:17,990
doing things in Python 2. So that's format.

78
00:05:18,000 --> 00:05:22,990
These are the things about it that are essential to know in order to be able use it.

79
00:05:23,000 --> 00:05:25,990
You can get a lot of power out of it just knowing what I have shown you here.

80
00:05:26,000 --> 00:05:28,990
And in fact, nine times out of ten, this is all you are going to need in order

81
00:05:29,000 --> 00:05:39,000
to use format effectively.

