#100DaysOfCode in Python Transcripts
Chapter: Days 34-36: Refactoring / Pythonic code
Lecture: Refactoring 7: string formatting and concatenation

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Right, next stop:
0:01 string formatting and concatenation.
0:03 And pretty important because you will be doing this
0:05 a lot in your code. It's funny the other day
0:07 I bought some clay with my daughter
0:09 and we were making python figures
0:12 and obviously we were very bad at it
0:14 and then we got better. And I found it a nice analogy
0:18 with string formatting. As the Zen says there's
0:20 preferably one best way to do it
0:22 and there are various ways to do string formatting
0:25 but in the end there is a best way
0:28 which is now the f-string in 3.6
0:30 but if you're not on 3.6 then at least you can use format
0:33 that's basically the gist for me of doing formatting
0:36 the right way. But let's also demo that with some code
0:39 And I hope you agree that those last two figures were
0:41 definitely better than the first.
0:43 Alright, total hours
0:45 is six
0:47 print.
0:48 the course
0:50 takes
0:51 plus total hours
0:57 to complete. Okay a type error, not good
1:01 so I cannot add an 'int' to a 'string'
1:03 and vice versa. So I need to make this a string
1:08 and then it works. But yeah, it's not the best way to do it.
1:12 And the best way is using f-strings in Python 3.6
1:20 and look at that I can just embed the variable.
1:23 This can even take operations, its pretty awesome
1:28 but yeah, this is faster and it's much easier to read
1:32 and I don't have to concatenate
1:33 or even doing any type conversion.
1:35 So go with f-strings, but maybe you're not on 3.6
1:38 for some reason and then you can use format.
1:41 So you would write this as
1:46 in the same curly braces
1:52 and then you use format on that string
1:55 and give it a variable. And you don't have to worry
1:58 about type conversion because format is,
2:00 sorry, not using f-strings anymore
2:04 format is smart enough to convert this 'int' into a 'string'
2:07 or whatever is needed to make this work.
2:09 And then last note about concatenation
2:12 so as said before, building up a string like this is
2:16 not the way, and its slower
2:18 if you're working with a lot of data
2:22 and how true is that, right?
2:24 Every day you get to write python is a happy day
2:27 but, it's not happy for your performance
2:31 and readability I would say, it's not really looking nice
2:34 in this case you really want to use join
2:37 so you want to really have your strings in a sequence
2:39 or a list, and then just join them together
2:42 and you can specify what to join on
2:46 and there you go, here's my same string as above
2:49 but using the Pythonic way of joining
2:52 and, you know, you can do what you want
2:54 you can also join it on dash, even better
2:57 I can leave off the spaces here
3:01 and give join a space.
3:03 So I'm going from multiple spaces
3:05 or worry about spacing in the first place
3:07 by doing that in one place at the join level
3:10 so much better.