String Concatenation and Formatting - Intermediate Python Programming p.2
Key Takeaways
The video discusses string concatenation and formatting in Python, covering topics such as the 'join' method, 'os.path.join' for file paths, and string formatting with curly braces. It provides examples and best practices for improving readability and scalability in Python code.
Full Transcript
hello everybody and welcome to part two of our intermediate Python Programming tutorial Series in this video we're going to be talking about string operations like string concatenation and string formatting these are some things that at least I personally didn't do very well and I don't think I think when you're learning Basics you just learn the simplest way to do it so uh to begin let's start with a list of names and we're going to say uh we've got a Jeff we've got a g G we've got a Jill and we have a Samantha just for the record I'll try to mention pep8 the first time that we cover it uh but in a list you just have your item comma space and then spaces around all equal signs except when you're putting them in function parameters just for the record anyway um so those are that's our name our list of names so if we let's say we want to iterate over this list and we want to say hello there and then the name of this person you might say something like this like for name in names print um and then we would say like hello there commas space uh or well we could do at this point we could say plus spaces renter pluses uh name like that you could also do hello there no space and then do a common name pull this down and you see hello there Jeff hello there Gary jills meth and all that so depending on who you talk to this is either the correct way or the incorrect way in terms of readability this is the most correct way uh because it's easily understood now there's a second way to do this and again depending on who you talk to there can be an argument as far as what's the right way it really doesn't matter you should just go with what you're more comfortable with but I'll show you when the second way is essential so um instead we could say print and um trying to satify yeah this will be fine so print and then we'll do um a space between the quotations and then we are going to join and then in join you put a list of things that you're going to join together and it's a space will be between these things that we join so you would write you you know hello there and then a comma and then the name so I'll comment this out now and then we'll run it maybe there we go that took a while see if I can get rid of that y there we go um okay so you got oh well we didn't we didn't get our comma but we can just add that in okay so so it's the same thing but if you ask me the first one is just so much more readable and I'm not really sure that it's worth the performance gain um here and as python changes over time these are things like subtle things like these that really are going to change uh by python even version sometimes so in some senses this they might be like the difference is probably just extremely negligible you'll get away with it but the difference is when say you've got a list of strings or really any anything and you want to just print them out as a string print that list as a string of course you could do string names and just print that but let's say instead you actually want to just print the list of the names as a string so that might look something like let me just comment this out whoops comment this out and that might look like um oops not again um we will join uh just as a comma do join and then names is a list so we're fine let's run that um and in fact let's do commas space there you go so now we've got Jeff Gary Jill smantha okay so that that works is it easily readable uh if you know how join works then sure it's it's it's readable but if you don't know how join works that's not readable at all uh but when when it comes to concatenating Strings join is preferable when you're joining more more than two strings uh some people will argue that maybe it's more than like some number of strings like when the processing changes but I think as a general rule of thumb if you're going to concatenate and truly concatenate because actually what we're doing here we are concatenating but we're still actually kind of doing the wrong thing in both of these examples I'll show you the right thing soon enough but probably this isn't even the right way to go about it but if you're concatenating two or more than two not two two or more so basically all concatenation no uh if you're concatenating more than two strings you probably should be using join purely because it's going to scale better it's going to use less processing now while we're on the topic of join I want to show one more thing that join can be used for or at least the idea of join um so we're going to go ahead and import OS and then we're going to say like consider that you have um a file located somewhere so let's say we've got location of file and I actually have a file here so it'll work you can either create a random text file somewhere and just put something in the text file or you can just watch it doesn't really matter um so C colon double backs slash because it's Windows doing Windows things so Windows likes to use backs slashes so we use back slashback slash to escape the backs slash um character anyway H backback slash desktop intermediate python so that's the location of f actually let's say it's the location of files because probably if it was just a file you would just give the oneoff path and you'd be fine but let's say it's an example where maybe you're going to iterate through all the files in some sort of os. list dur format so you're listing out the dur you get the file names and then you want to open them up so you're going to start with the location of the file and then you're going to grab that file name let's say the file name well it is in my case example. text it's tempting to let's say we're just going to print we're not going to open it right now but it might be tempting to say location of files plus um actually let's do the back slashback slash to be all proper here um plus file name so that's really tempting to do right and then boom yeah you've got the full file path um but again that's not the right way to go about it and a lot of times especially with join uh you're going to have maybe more than this because each directory could in theory be its own little string and there's actually going to be a lot of times where you might use that if you are working in the in the system itself um so you wouldn't necessarily have a full path you might be actually joining C colon users H well probably not see SL let's say you're joining users to H to desktop to intermediate Python and and so on um but this will just be a single join um the way that we can actually do that instead is you'll see something like this like with open and then we're going to say os. path. jooin it's going to work just like the other join only it's just going to automatically add the little slashes for us so [Music] join come on man location of files uh and then the file name which has an underscore um and then uh with open uh since we're just going to read we don't have to specify how we're going to open it so as F and then we're just going to print f. read save and run that and that's the contents so it just says oh higher great so that's join and we still have one last thing and that's going to be string formatting so actually um the location of files that's a that's what you would do really you would use a join you're not going to use string formatting but now this this top one with Jeff Gary and all that um that was probably that would it would make more sense to use string formatting most likely with that but I just wanted to show a really simple example so let's go ahead and talk about stream formatting now so let's say you want to have a sentence with the following variables let's say who equals Gary um and then how many will equal 12 and we're hoping for a sentence to say something like Gary bought 12 apples today okay uh where Gary is the variable and 12 is also a variable so this can be really tempting to do the following like um uh in fact let's do sentence equals something and then we'll just print the sentence so let's say you know you might want to say like who comma [Music] um bot comma How many comma let's add the spaces after these we'll be pep eight guys [Music] um actually we need this to go in the print statement the format will work but this won't work so anyways who bought how many uh and then uh we'll say apples and then space today all right so we can run that and that might be one way you might also rather than the commas you might add the spaces and then use the plus to each his own but that's wrong and Once Upon a Time you could use the percent s for string formatting and that was thought to be the the best way to do it uh that's no more uh instead what we have are the curly braces why did it do that interesting I don't know why that's a comment so the fact that it would error there I don't know that's interesting why is that happening anyway um not there just on the comment anyway um Idol typical so the real way you should do this is print and then you would say um just empty brackets spacebot empty brackets apples today and then you will say format and then just throwing in all of the parameters in this case we just have two which is who and how many so we can run that uh and we should get the same sense good so both of them G about 12 apples today this is for the moment the correct way to do string formatting um if you're following along and you're in Python 2 you have to give like values like so one and two and I think it'll work in three no that's not going to work I wonder if I have to put this I'm not sure how to do it in two but just know if you're following along in two stop that upgrade to three but if you refuse um there's something specific about python 2 with string formatting stram let see if I can find it really quick come on someone give me an example so oh I said one two okay yeah duh all right so 01 let's run it boom okay so you can do 01 So in theory you could say instead one zero and let's see if the functionality runs through and it says yeah 12 bought Gary apples today so you if you want to do order I'm not sure why you would really need to do that I guess it's a little more explicit but it's unnecessary if you ask me um but in Python 2 you'll have to use the index values I can't believe I tried one two oh come on come on man anyway I need to go back to the basics and learn that lists start with zero with anyway uh okay so that's string formatting and um why you should upgrade to Python 3 so you don't have to put numbers in the curly braces I me come on so anyway that's enough on string formatting since I have it up I mean there is a ton of stuff you can do first of all you can upgrade to three oh yeah looking good okay so there's a ton of stuff that we can do with string formatting what I just showed you was like really basic um but these this is these are two things that I see violated the most and I personally violated the most myself uh just concatenation with pluses or commas um especially with file names I was really happy when I found out o os. path. jooin uh also just string formatting with the curly braces that works I think it's very readable um that's the way to go that's the most scalable way so uh that stream formatting that's all for now if you have questions comments concerns whatever feel free to leave them below otherwise till the next video
Original Description
Welcome to part 2 of the intermediate Python programming tutorial series. In this part, we're going to talk about strings. Strange that we find such a topic in an intermediate series, but it's probably one of the most common things done improperly.
https://pythonprogramming.net
https://twitter.com/sentdex
https://www.facebook.com/pythonprogramming.net/
https://plus.google.com/+sentdex
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from sentdex · sentdex · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
Python Encryption Tutorial with PyCrypto
sentdex
Python's Logging Function
sentdex
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
wxPython Programming Tutorial 4: Panels
sentdex
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
wxPython Programming Tutorial 8: Custom Button Images
sentdex
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
Basic PHP Tutorial 12: Associative Array
sentdex
Basic PHP Tutorial 14: Foreach loop
sentdex
Basic PHP Tutorial 16: Include and Require
sentdex
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
Basic PHP Tutorial 4: Variables and Comments
sentdex
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
Basic PHP Tutorial 1: Intro to PHP
sentdex
Basic PHP Tutorial 3: HTML with PHP
sentdex
Basic PHP Tutorial 9: While Loop
sentdex
Basic PHP Tutorial 10: Switch Statement
sentdex
Basic PHP Tutorial 2: Print and Echo
sentdex
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
Basic PHP Tutorial 19: Finding words in strings
sentdex
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
Basic PHP Programming Tutorial 24: String similarity
sentdex
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
Sikuli Tutorial 1: Visually programming in python!
sentdex
Sikuli Tutorial 2: Program visually in python!
sentdex
Sikuli Tutorial 3: Program visually in python!
sentdex
3D Bar Charts in Python and Matplotlib
sentdex
3D Plane wire frame Graph Chart in Python
sentdex
Raspberry Pi Part 1 Introduction
sentdex
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
Raspberry Pi Part 11: Remote Desktop
sentdex
Twitter Analysis: How to rank a user's influence
sentdex
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex
More on: Prompt Craft
View skill →
🎓
Tutor Explanation
DeepCamp AI