Python Tutorial Python Advanced Python References Python Libraries

Python String - format() Method



The Python format() method returns the formatted version of the given string where the specified values are inserted into the string's placeholder. The placeholder is defined using curly brackets: {}.

Syntax

string.format(value1, value2...)

Parameters

value1, value2... Required. Specify one or more values that should be formatted and inserted in the string. The values can be of any data type. The values can be positional arguments or keyword arguments, or a combination of both.

Return Value

Returns the formatted version of the given string.

Example:

In the example below, format() method returns the formatted version of the string called MyString.

#default arguments
MyString = "I am {} and I am {} years old."
print(MyString.format("John", 25))

#positional arguments
MyString = "I am {0} and I am {1} years old."
print(MyString.format("Kim", 29))

#keyword arguments
MyString = "I am {name} and I am {age} years old."
print(MyString.format(name="Marry", age=22))

#mixed arguments
MyString = "I am {0} and I am {age} years old."
print(MyString.format("Sam", age=28))

The output of the above code will be:

I am John and I am 25 years old.
I am Kim and I am 29 years old.
I am Marry and I am 22 years old.
I am Sam and I am 28 years old.

Formatting Types

Inside the placeholders, the following formatting type can be used to produce the result in the desired format:

Formatting TypesDescription
:< Left aligned to the remaining space.
:> Right aligned to the remaining space.
:^ Center aligned to the remaining space.
:= Places the sign (+)(-) to the left most position.
:+ Use a plus sign to indicate if the result is positive or negative.
:- Use a minus sign for negative values only.
: Use a space to insert an extra space before positive numbers (and a minus sign before negative number).
:, Use a comma as a thousand separator.
:_ Use a underscore as a thousand separator.
:b Binary format.
:c Converts the value into the corresponding Unicode character.
:d Decimal integer.
:e Scientific format, with a lower case e.
:E Scientific format, with an upper case E.
:f Displays fixed point number (Default: 6).
:F Same as 'f' except displays inf as INF and nan as NAN.
:g General format. Rounds number to p significant digits. (Default precision: 6).
:G Same as 'g'. Except switches to 'E' if the number is large.
:o Octal format.
:x Hexadecimal format (lower case).
:X Hexadecimal format (upper case).
:n Number format.
:% Percentage format. Multiples by 100 and puts % at the end.

Example:

The example below describes how to use formatting types with the format() method.

#using decimal integer format
MyString = "Hello {}, your balance is {}."
print(MyString.format("John", 1000.23))

#using binary and hexadecimal format
MyString = "25 in binary format is {:b}, and in hexadecimal format is {:x}."
print(MyString.format(25, 25))

#using right align format
MyString = "{:>5}"
print(MyString.format(25))

#using padding for float number
MyString = "{:.2f}"
print(MyString.format(25.125125))

The output of the above code will be:

Hello John, your balance is 1000.23.
25 in binary format is 11001, and in hexadecimal format is 19.
   25
25.13

❮ Python String Methods