suppose we have a dictionary and this is our 2
target 3
we want to generate this kind of string 4
here the point is how can you merge the 5
values from the dictionary 6
into this particular string at specific 7
place 8
for example this value here 9
guess how to do that if you say 10
string concatenation with plus operator 11
you are wrong 12
concatenation is a costly process 13
for example if you try a plus b plus 14
c it will generate two individual 15
strings unnecessarily 16
for a plus b one result will be 17
generated 18
and that string again plus c 19
so this hole will generate another 20
string 21
like this for every usage of place operator 22
a new string will be generated so it is 23
not that much memory efficient 24
so what are the alternative for the same 25
of course f strings is a good choice 26
here 27
but let's find out if there are any 28
other options 29
in this dictionary project let me create 30
a new file 31
format and format map 32
dot py 33
let me create a dictionary first so my 34
goal here is that 35
i would like to generate this particular 36
string 37
here i would like to add the value of 38
the name 39
and in this place of course the value of 40
job 41
at first let us try with f strings 42
because already we know that 43
so prefix the character called f before 44
the double quotes 45
and then you can use the braces to 46
replace the actual 47
value of a variable in this case you are 48
getting the value from 49
employee especially which key 50
the name key 51
so this particular portion of the code 52
represents the actual value 53
from that name key that is jackson 54
and then after that you can continue 55
writing your own literal text 56
let's say for example is a 57
and then after that you would like to 58
add the value from 59
job key so we have to add 60
again price and in this brace we can add 61
the value from 62
employee of the job key 63
see we cannot nest the double quotes 64
already for outside we are using double 65
quotes 66
so we have to try using alternative that 67
is single quotes 68
for getting the value of the key 69
like this so this has generated our 70
target string that is jackson is a 71
developer 72
that's fine but in strings 73
we have alternative methods that is 74
format and 75
format map that is exclusively meant for 76
dictionaries only 77
so let's have a look 78
at first let us try format method 79
so in this case we have a string 80
and we have to say dot 81
that means first of all we are trying to 82
create a string object 83
with these double quotes and then from 84
this string object 85
we are calling the method called format 86
and then we require to provide the 87
actual dictionary that you want to read 88
so which dictionary you would like to 89
read that is 90
employee but this dictionary cannot be 91
read as it is 92
we require to provide some reference 93
name or nickname for example emp 94
means employee 95
so this emp acts as a nickname 96
temporarily and 97
this can be used to access that value 98
from dictionary 99
and this nickname available only for a 100
short period of time 101
until the execution of this format 102
method 103
so once this statement has been 104
completed and we go to the next line 105
that nickname or temporary key will not 106
be available 107
so only within this particular string 108
that short key is available 109
so now inside the string we are writing 110
the braces because 111
we would like to represent the value 112
from that dictionary 113
and we can access that dictionary not by 114
employee 115
but by writing emp so in this emp 116
which key you would like to get that is 117
name 118
but wait already we are able to do that 119
in the f strings right 120
but only the advantage of this format 121
method is that 122
we can create this kind of short name 123
and also we can access that key without 124
codes 125
see in case of f strings we should write the key 126
only inside the codes may be single 127
quotes 128
but that is not required in case of 129
format method 130
so directly you can access that key by writing 131
the key name itself in this case 132
all these keys of this dictionary acts 133
as a variables 134
so directly you can access like emp of 135
name 136
here emp is the short key that we have 137
created here 138
that represents the entire dictionary 139
and after that name is the key here 140
so this portion of the code represents 141
the actual value from that key 142
that is the action 143
then after that you can continue writing 144
your own literal text 145
something like this and one more time 146
you would like to get the value from that dictionary right 147
so again key braces this braces will not 148
be printed in the string 149
but it indicates that you would like to 150
get the value from that dictionary 151
dynamically 152
so again we are accessing that 153
dictionary by using 154
the short name that is emp 155
and then you can use the key without 156
code that is 157
job and that's it 158
we will get the same output as it before 159
so the only main advantage of this 160
format method is 161
you can use the keys without codes 162
that is probably not possible with f 163
strings 164
but i really feel this format method is a bit 165
messy and confusing it's not that much fluid 166
as f strings 167
so let's find out some better ways than 168
this 169
that is format map method 170
so instead of using format we are using 171
format underscore map 172
in this case we cannot give the short 173
name like this 174
and we can just supply the dictionary 175
itself 176
the biggest advantage of the format map 177
is 178
you don't even require any short name 179
for this dictionary 180
you can absolutely directly write the 181
key that you want 182
and that's it you can directly access 183
the value from that key 184
so in this dictionary at the key called 185
name the value is jackson 186
so directly you will be able to access 187
that value 188
in this place isn't it easy 189
so again here as well you can simply write 190
job that is the key 191
so finally both f string and format map 192
both are good you can use any of these 193
based on your requirement 194
i really feel format method makes more 195
sense 196
because easily you can access the value 197
see in case of f string as well we have to write 198
the dictionary name and then square 199
brackets and then chords 200
and then key so let the process right 201
but in case of format the same is going 202
to be very easy here 203
that is why in general if you want to 204
generate the string based on the 205
dictionary values 206
the format map is the better choice 207
and this is the cheat sheet for the same 208
you