sec01 - Basics of the format() method

In this lecture, we will learn the basics of the format method and how to define it.

For formatting specifications such as digit grouping, the content is the same as f-strings, so we will learn more about them in the lecture on “String formatting (format specification).”

スポンサーリンク

Replace in the order of the arguments [“positional arguments”]

When using format, first create a “template string.” In the template string, insert {} to indicate where variable values will be replaced.

template = 'I am {}, {}.'

Template strings are treated in the same way as normal strings, so you can use the format method provided by the String type.

template = 'I am {}, {}.'
text = template.format('Nico', 10)

When you print the above code, “I am Nico, 10.” will be output. The values of the arguments of the format method are replaced in the positions indicated by the curly brackets in the template string. (The values are replaced from left to right in order.) When using the format method, you can concatenate characters without converting numbers to the String type.

Positional Arguments

An argument is a value passed to a function, specified within parentheses. In the case of print(‘Python’, 3), 'Python' and 3 are arguments, and when there are multiple arguments, they are specified separated by commas. The print function performs the process of outputting the received arguments to the screen.

In Python function definitions, arguments that define values in order in the parentheses of a function are called “positional arguments.”

This will be explained in detail in the lecture on functions.

To confirm that the position of the arguments is important, let's change the order of the arguments and execute the program.

template = 'I am {}, {}.'
text = template.format(10, 'Nico')

When you print the above code, “I am 10, Nico.” will be output. If the output is not as expected when writing the code, check the position (order) and number of {}.

When actually using the format method, the values to be substituted into the {} placeholders are not fixed, so you need to define them as variables. (If the values are fixed, you can simply write them directly as strings.) Therefore, in practice, you will replace each placeholder with the value assigned to the corresponding variable, as shown in the following code.

name = 'Nico'
age = 10
template = 'I am {}, {}.'
text = template.format(name, age)

Also, the above code assigns the template string to a variable once, but you can just write .format() right after the template string, and it'll work fine.

name = 'Nico'
age = 10
text = 'I am {}, {}.'.format(name, age)

Replace by specifying a name ["keyword arguments"]

Next, we will explain how to name curly brackets. Enter the name inside the template string {} as shown below. Give the name a name that makes it easy to understand what value will be replaced in {}.

template = 'I am {name}, {age}.'

The arguments of the format function are written as (name=value), linking the name specified in curly brackets with the value to be substituted for the curly brackets.

template = 'I am {name}, {age}.'
text = template.format(name='Nico', age=10)

When you print the above code, “I am Nico, 10.” is output. If you specify name='Nico’, age=10 as the arguments, ‘Nico’ is replaced with {name} and 10 is replaced with {age}.

Unlike “positional arguments,” the location to be replaced is specified by name, so even if you specify the arguments in the reverse order of the previous code, the replaced string will be the same.

template = 'I am {name}, {age}.'
text = template.format(age=10, name='Nico')

The print output of the above code is the same as last time: “I am Nico, 10.” The order of the arguments is reversed from before, being written as age=10, name='Nico', but 10 is replaced with {age} and 'Nico' is replaced with {name}, so the same result is obtained.

Keyword Arguments

In Python function definitions, arguments defined as name(variable_name)=value are called “keyword arguments.” We will explain this in more detail in the lecture on functions.

スポンサーリンク