Python String format() Method with Examples

String format() Method in Python:

The format() method formats the specified value(s) and inserts them inside the placeholder of the string.

Curly brackets{} are used to define the placeholder. In the Placeholder section, you can learn more about the placeholders.

format() returns the formatted string.

Syntax:

string.format(value1, value2, .......)

Parameters

value1, value2, ……. :

Required. One or more values to be formatted and inserted into the string

The values are either a comma-separated list of values, a key=value list, or a combination of the two.

Any data type can be used for the values.

For Example:

Approach:

  • Apply the placeholder for the cost variable and store it in a variable.
  • Apply the format() function for the above string by passing the cost as some random number and print it.
  • The Exit of the Program.

Below is the implementation:

# Apply the placeholder for the cost variable(static string)and store it in a variable.
gvn_str = "The dress cost is {cost:.2f} Rs."
# Apply the format() function for the above string by passing the cost as some
# random number and print it.
print(gvn_str.format(cost=850))

Output:

The dress cost is 850.00 Rs.

Placeholders

The placeholders can be identified using named indexes such as {cost} numbered indexes such as 0 {0}, or even empty placeholders {}.

For Example:

Approach:

  • Apply the placeholder for the ename and id (static string) and apply the format() function for the given string by passing the ename and id with some random values and print it.
  • This is in named indexes format.
  • Similarly, do the same by numbered indexes format and empty placeholders format.
  • Print the results separately.
  • The Exit of the Program.

Below is the implementation:

# Apply the placeholder for the ename and id (static string) and apply the format()
# function for the given string by passing the ename and id with some random values
# and print it.
# (This is in named indexes format)
gvn_str1 = "The Employee name is {ename}, and his id is {id}".format(
    ename="virat", id=28)
# Similarly, do the same by numbered indexes format and empty placeholders format.
# Print the results separately.
gvn_str2 = "The Employee name is {0}, and his id is {1}".format("virat", 28)
# empty placeholders format
gvn_str3 = "The Employee name is {}, and his id is {}".format("virat", 28)
print(gvn_str1)
print(gvn_str2)
print(gvn_str3)

Output:

The Employee name is virat, and his id is 28
The Employee name is virat, and his id is 28
The Employee name is virat, and his id is 28

Types of Formatting

:<        – The result is aligned left (within the available space)

:>        – The result is aligned right (within the available space)

:^        – The result is centered (within the available space)

:=        – Positions the sign to the far left.

:+        – To indicate whether the outcome is positive or negative, use a plus sign.

:-        – Only use a minus sign for negative values.

:         – Insert an extra space before positive numbers by using a space (and a minus sign before negative numbers)

:,         – As a thousand separator, use a comma.

:_       – As a thousand separator, use an underscore.

:b       –  binary format

:c        – The value is converted into the corresponding Unicode character.

:d         – decimal format

:e         – Scientific notation in lower case e

:E        – Scientific notation in upper case E

:f         – Fix point number format

:F        – Fix point number format, all uppercase (show inf and nan as INF and NAN)

:g         – General format

:G        – Format in General (using an upper case E for scientific notations)

😮        – octal format

😡         -Hexadecimal, lower case

:X          -Hexadecimal, upper case

:n          – format for numbers

:%         – Format in percentages