In this tutorial, you will learn:

What is Python String format()? Syntax of format() function in Python How string format() works?
Example: Empty Placeholder replaced with a string value
Example: Empty Placeholder replaced with a numeric value
Example: Using variable or keyword arguments inside the Placeholder
Example: Using index or positional arguments inside the Placeholder
Formatting inside Placeholders
Using class with format()
Using dictionary with format()
Padding Variable Substitutions

Syntax of format() function in Python

Parameters

val1, val2 … : The values that need to replace in the given template string that has placeholders in the form of curly brackets {}. The placeholders can be a string, key/value pair, integers, floating-point numbers, characters, etc.

Return value:

It will return the final string, with valid values replaced in place of the placeholders given in curly brackets.

Placeholders

The placeholders in the template string are represented using curly brackets, e.g. {}. The placeholder can be empty {}, or it can have a variable for e.g {name} , or it can have a number index e.g {0} , {1} etc.

How string format() works?

The Python String format() method will scan the original strings for placeholders. The placeholders can be empty curly brackets ({}), positional arguments i.e the string can have placeholders with index 0, 1 for e.g {0}, {1} etc. For keyword arguments the variable name will be present inside the curly brackets for e.g {name}, {age}. In the case of empty curly brackets, the values from the format will be replaced inside the curly brackets in sequence. The first value will be replaced with the first empty curly bracket, followed by the next one. For positional arguments, the index will start from 0 and so on. The values will be available in format separated with commas, and the 0th value will point to the first value inside format and so on. For Keyword arguments, i.e., when you use a variable inside your placeholders, you can have the order of values inside the format as you need. The order does not matter here as the values will be replaced based on the variable name present in the format(). Here are a few examples of how to use placeholders inside a string as empty, positional, and using keywords or variables.

Example: Empty Placeholder replaced with a string value

In the example below, the string has empty curly brackets({}). The value given to the format will get replaced inside the curly brackets({}). The value that we want to be replaced is a string. Example: Using String Formatting in Python, we want the curly brackets ({}) to be replaced with a string value. The value is given to format(“Guru99”). On execution, the curly brackets {} is replaced with Guru99, and you will get the final string as Welcome to Guru99 tutorials. Output:

Example: Empty Placeholder replaced with a numeric value

In the example below, we want the numeric value to be replaced inside the original string. The curly brackets({}) are added to the place where you need the numeric value. When it executes, the empty curly brackets ({}) is replaced with the numeric value. Example: You can also make use of format() to insert numbers inside your string. The example will show how to replace the empty Placeholder {} with number 99 present inside format(). Output:

Example: Using variable or keyword arguments inside the Placeholder

It is also possible to make use of variables inside the curly brackets, as shown in the example below. The variables are defined inside format(). Therefore, when it executes, the value assigned to the variable is replaced inside the original string. Example: You can use variables inside curly brackets for example {name} {num}.The values for name and num variable are available inside format i.e. format(name=”Guru”, num=”99″). The values given to name and num will be replaced inside the {name} and {num}. Output:

Example: Using index or positional arguments inside the Placeholder

The values can be replaced using indexes like 0, 1, 2 inside the placeholders. The value will be picked in order from the format(), as shown in the example below. Example: Output:

Example: Using multiple placeholders inside a string

In this example, we are going to use multiple empty placeholders. Example: The string used in the example has multiple empty placeholder and each placeholder will refer to a value inside format() .The first value will be replaced for the first placeholder and so on. Output:

Formatting inside Placeholders

You have seen that it is possible to have Placeholder as empty, with a variable or an index. It is also possible that you can apply Python String Formatting inside the Placeholder. Here is the list of formats

Using class with format()

In this example, we are creating a class and use the object of the class inside the .format Python method. The placeholders will refer to class properties or members using the class object. Example: Showing output upto 2 decimal places. print(“The value is : {:.2f}".format(40)) Output: This example shows how to skip the decimal places by using {:.0%} inside the placeholder. print(“The value is : {:.0%}".format(0.80)) Output: The comma(,) is added , as a thousand separator as shown in the output. Output: Output: Output: Output: Output: Here, you can use 10 that will add 10 spaces in the final text, and the value to be replaced will be center-aligned between the 10 spaces. The spaces of 10 are added just to show the center alignment of the replaced value. Output: Output: Example: The class is called inside the format(c=MyClass()).The object c will have the reference to the properties and methods inside class MyClass(). Output:

Using dictionary with format()

It is also possible to make use of dictionary inside format() as shown in the example below: Output:

Padding Variable Substitutions

Using string.format() method, you can add padding, space by using placeholders inside your string. Example: In below example will add space inside the Placeholder using the format(). To add space, you have to specify the number of spaces inside curly brackets after the colon(:). So the Placeholder will look like {:5}. Output: You can also give the index inside the placeholder for example: {0:5} where 0 will refer to the first value inside format. Output:

Summary

Python string class gives us an important built-in command called format() that helps us to replace, substitute, or convert the string with placeholders with valid values in the final string. The placeholders inside the string module Python are defined in curly brackets, e.g., “Welcome to Guru99 {}”.format(‘value here’). The placeholder can be empty {}, or it can have a variable for e.g {name} , or it can have a number index e.g {0} , {1} etc. You can make use of String Formatting in Python inside placeholders that can help to add padding, center align, and also help with number formatting.