Skip main navigation

The format method with *args and **kwargs

Learn about the built-in function that uses both `*args` and `**kwargs`, namely the `format` method of string objects.
12.2
Hi. Welcome back. I think this will be our last lecture on positional arguments, key-word arguments, et cetera. And I just wanted to look at a built-in function that actually uses the asterisk args and the double asterisk keywords in the function definition. So what I mean is like we saw up here. So what’s a function that actually uses this - just like this function here - what’s a function that actually uses this double asterisk to collect all the keyword arguments and also uses the asterisks args to collect all the positional arguments and then do something with them? So one function is the format method of a string object. So this is one of the preferred ways to format strings.
60.5
You’ve seen it before. There’s a newer way, called F-strings. So there are multiple ways to format strings in Python. This .format method used to be the older, preferred way. F-strings are now the new preferred way, but not all versions of Python have F-strings, only the more recent versions do. So right now, we’re using this .format. You’ve seen this before but let’s just review it again. So here we have a string, and everywhere that there is the curly brackets, we’re going to replace it with a value we pass to the format method. So if I run this cell, you see the output is “Hi, my name is Will.” See how this was curly brackets, but it was replaced by “Will.
104.4
I live in California.” Again curly brackets replaced by “California,” and then the last is the UCI.
114.9
So, again, this is a string we call the format method. So you write your string, then .format. And then you just pass positional arguments in the order you’d like to replace them in the string.
130.2
So the point, here, is that format, it can accept any number of positional arguments. Now if you don’t have enough curly brackets to replace them, you’ll get an error. It’ll basically say there’s a mismatch between the number of areas in the string you want to fill in - those are the curly brackets - so the mismatch between the number of curly brackets and the arguments. But as long as those match, you can have as many arguments as you want to pass to format. So how does it do that? It’s using this asterisks args. So in the definition, it’s using this to collect all the positional arguments and then go through the string and replace them. It also uses kwargs.
170.8
So another way that you can format a string is you can actually specify the name of what will replace it. So here we have a string, and you have the curly brackets again, but inside the curly brackets, you have “name,” and here you have “home,” and here you have “work.” So we are now actually labelling the names of the variables we want to use to replace. So there’ll be a name here, a variable called home here, work here. And those you would pass as keyword arguments. So the name in the curly brackets matches the name of the keyword argument.
207.2
And you can have anything here. So these keyword arguments are not precoded into format. It doesn’t know you might pass name - it could be anything. In fact, we could write “anything” and just change this to “anything,” and it will still - oops, I made a misspelling - there we go - and it’ll still work. So what is it doing? It’s using the double asterisk keyword arguments in the function definition. So this “double asterisk keyword arguments,” it’s capturing all the keyword arguments and then it’ll go through the string and it will match the argument to what it’s replacing. So the definition of format uses both of these - args and kwargs.
250.8
Actually, I should say, it uses a single asterisk and double asterisk. Again, if you use args or some other name, doesn’t really matter, although args is almost always what’s used. The real important part is this single asterisk and the double asterisk. Let’s take one more look at that. So format is going to be like this function, except it would only take args and kwargs. So format will look something like - not necessarily exactly - but it’ll look something like this. So it’s accepting all positional arguments and all keyword arguments. And then inside the code, it will iterate through those to fill in the string as appropriate.
300
All right that’s it. I just wanted to leave you with an example, a real working example, of a very heavily used function that uses the asterisks args and the double asterisk kwargs in real code. All right, great job, and I will see you in the next lecture.

You have learned a lot about positional arguments, keyword arguments, *args and **kwargs, and we have created some simple functions to help understand these concepts.

In this video, we look at a built-in function that uses both *args and **kwargs. The function is the format method of string objects.

The .format() method of a string object.

One of the preferred methods to format strings in Python is to use the format method of string objects. (The latest preferred method is something called f strings).

This is demonstrated by the example in the video:

  • First, we define a string and we put {} in the string wherever we would like to fill in the string by a variable.
  • We then call the .format() method on the string and pass to it the variables we would like to use to fill in the {} portions of the string.
  • The {} are filled in by the order we pass the variables to the .format() method.

Follow along

The file used in this video is Function Positional and Keyword Arguments.ipynb. If you have not already done so, please download this file from the Downloads section in Step 2.2 – What is happening this week?.
Make sure you are able to access it, in order to follow along with the video.

This marks the end of the activity. In the next activity, you will have the opportunity to complete a quiz and comment on your experience for this week of the course.

This article is from the free online

Intermediate Python

Created by
FutureLearn - Learning For Life

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education