 Hi everyone going to show you a quick video on string formatting handy-dandy tip Came about in one of the newer versions of Python for working with strings when you want to print them out and trying to get Them to look right so we are working on string formatting right now I'm in my pie charm editor and As you recall strings are sequences of characters Wrapped in single quotes or double quotes, right? So strings are a great way of capturing data So for example, I'm going to create a variable and assign to it my name as a string, right? and I will also create a different variable which is an age and assigned it an Integer value so pie charm is nice. It highlights the different variable types in different ways But very simply here's a nice string, right? So now let's say we want to print out this string or this combination of information, right? so I want to print to the screen to the console something like hi, my name is Give me the value of name And I am age Years old that's what I want to get out of it Okay, so you may have learned a couple of different ways to get This printed to the screen using the values of these variables So one option is to do something like this We want to print and when you print you usually are printing a string. So we want to combine The string we want to print with this other information So hi, my name is and then you may have seen something like plus name right the plus operator When paired with these two things? This is a string. This is a string plus means concatenate bring them together Hi, my name is plus name and Then continue with what I wanted to say and I am and now I want the age in there. I am age 30 haha years old years old so I'm going to run this and you can run this a number of ways you can Go up here and click the run button and pick your file. You can right-click Run string format. I am going to use the keyboard shortcut control shift F10 and This did not work for me, right? Can only concatenate string To string not an integer. So it's screaming at me here. Python's very unhappy Because I did this poorly. It's mad because this thing age is an integer and The rest of this stuff is a string and it's telling me it's trying to tell me here I don't know how to concatenate an integer to a string. I just don't know so one solution is to cast This age this integer value to a string and now it will work Okay, no big errors, but you can see it's kind of strangely formatted here And that's because I forgot my white space around here This is a very clunky way of dealing with this. I have to worry about my white space. I have to worry about Maybe trying to put things in my string. I want to print out that aren't strings Not great solution. There's another solution a little bit better We can do it this way print hi my name is Okay, and now I'm going to put a comma here and The variable name Okay, so what this will do. Let's just run it This printed out hi my name is and then by doing the comma and the name It gave me a little space and it put the value in here for me. So it's not I Am not explicitly concatenating a big string like in this one The print statements doing it for me and that makes things a little simpler Because now I can keep adding Parameters to or arguments to this function and what's really cool about this way is I can put age in here And I don't have to worry about Converting it to a string it worked just fine, right? I don't have this wrapped in a string. So this is better Better solution. I'm treating this like a method that takes many parameters and What the print method is doing is it's turning everything every? Argument into a string and I'm putting them together separated by spaces. So that's nice. That's pretty good But what's even better and what I really want you to use is what's called an F string or a format string F string The way we make an F string is we are going to put F right here and then we're going to build our string inside it So let me start hi my name is and now here's the cool part. I'm going to put squiggly braces here squiggly braces and inside these braces I Can put an expression in this case. I'll just put the name variable Hi, my name is name value of name and I am Squiggly braces age years old Okay, so let's run it Here's the third one right and the cool thing about this is so because it's inside these squiggly braces it got the value of the name variable out and Converted it to a string it got the value of the age variable out and converted it to a string And the nice thing is the string is right here. It's all together. It's all in one place I don't have these little I don't have these plus signs these extra spaces this these commas and things that we have in this example Looks nice, right? So this is what I want you to get in the habit of doing if you've got a print something and you need to print a value of something Use one of these F strings. Okay. It's all there is to it They're also pretty flexible you can do more with them and it doesn't just have to be when you're printing You can format strings anytime. So let's see an example here. So let me create an empty list just for the heck of it and In this empty list, I am going to append a value to the end of my list I can append a string to the end of my list, right and I can use an F string So let's append let's create a string where it's got a name and then the age, right? So when I run this it's going to take the value of the name variable, which is Lucas and the value the age variable create a string That looks like named age, so it'll look something like like that and Append it to the end of the list okay, let's uh Let's print the length of this list and then let's print the first item in the list, which is at index zero Okay, so here's the string I inserted into my list and the way I formatted it was using this thing Another really cool thing here. Let me do this down here. Let's add another String to our list. Let's append another value You can make these expressions Inside these squiggly braces Complicated you can have them do calculations. So let's say I want to add on something to this so print name Plus I'm going to do some string concatenation here uh layman Now it's mad at me because Why is it mad? It's mad because I've got double quotes out here And double quotes in here. That's a no-no Double quotes it thinks that this double quote goes with this guy and that's not right Let me put single quotes that'll fix the problem Okay Name plus layman. So inside these squigglies It's going to take name and then concatenate to that string layman and then for age We can do a calculation age is an integer. It's 30. Let's be more realistic and add 10 to that Okay So this will append this new string that we've created and let's print it to people sub one There are two things in here now Right, so here I've got I created this string Appended it to the end and you can see I did some extra evaluation in here All right, all this is doing all this f string is doing is giving me back a string that substitutes Variables whole expressions figures out what they are and then turns them into a string very handy Even if you're just using it simply get in the habit of using f strings You