From the course: Python Standard Library Essential Training

String formatting - Python Tutorial

From the course: Python Standard Library Essential Training

Start my 1-month free trial

String formatting

- [Instructor] So far we've seen how to perform some basic string operations like filtering, searching and manipulation. In this example we'll learn how to format strings using a variety of Python functions. There's several different ways to format strings in modern Python and I'm going to demonstrate the three main ways of doing so, you can use template strings, the string format function and as a Python 3.6 there's a feature called string interpolation which I'll show in the next example. So let's open up strformat_start and we'll begin with template strings. So template strings have the advantage of being easy to use but with the downside of being not as powerful or flexible as the other two methods. So to use templates I first need to import the template class from the string module. So from string I'll import Template. Okay so I have my sample string here and I've got two place holders that marked with a dollar sign character and these placeholders will be substituted with variable names that I supply to the template, so to do that I first have to create the template and I'm going to use my Template class and pass in the str as the source of the template, then I call the substitute function on the template with my variable names to perform the replacement so I'll make an output_str and I'll call the templates substitute function and I'll substitute animal equals fox and then action equals jumped, and then we'll print the output. So let's go ahead and let's run that and you can see that in the output the variables have been substituted so here's the fox and the jumped. I can perform the same function with a dictionary object as well, so let's make a dictionary and I'll supply animal and this time I'll say it's a cow and then action will be walked and then I'll just copy and paste this and this time for substitute I pass in the args array or the dictionary. So let's clear this and now let's run and now you can see the substitution in the first case it's fox and jumped and in the second is cow and walked. All right so now let's try looking at the format function and before I do that let's clear this and let's comment out our previews example. So the format function is pretty powerful and I can't go into every detail here so I suggest you read up on it later to learn more about the power and the flexibility it provides. So here I've got two variables and I can format them into a string by using a basic syntax. So I'll print Output and then I'll have two curly braces and then I'll call the format function and in the format function I simply pass the names of the variables in this case just foo and bar. So this will take the two variables in order and put them into the string where the curly braces occur. I can also change the order of the output by using numerical indexes inside the braces. So I'll put a one here and a zero here and now the bar argument will appear first and foo will be second. So let's go ahead and run that and you can see that that's exactly what happens. I can also perform substitution with named variables so let's try that out. So I'll call this var1 and this var2, in this case I'll call format with var1 is equal to foo and var2 is equal to bar and if I want I can use format interactives to specify how pieces of data should be formatted. So if I wanted this integer number to be formatted as hexadecimal I can do that by using a formate interactive so let me just copy this and paste it and what I'll do is I'll have var2 for lowercase hexadecimal and then I'll use uppercase x for uppercase hexadecimal and I'll include a var1 as well. All right, okay so let's run the updated code. You clear this and we'll run it and there you can see the results in the output. So here is the formatting as hexadecimal and here is my variable name substitution. Okay so that's a look at how you can use string templates and the format function. In the next example we'll take a look at a new feature called string interpolation.

Contents