Skip main navigation

New offer! Get 30% off one whole year of Unlimited learning. Subscribe for just £249.99 £174.99. New subscribers only. T&Cs apply

Find out more

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.

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

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now