Menu

Understanding Python String format()

Python String format()

In Python, String format() is an inbuilt function with the help of which formatting of string can be done which leads to better output.

  • With the help of this function, one is able to format some selected parts of a string.
  • This function returns the formatted string with the value passed that is passed as a parameter in the placeholder position.
  • This method is useful for concatenating elements within the string with the help of positional formatting.
  • To control some values Like input and values from the database (which are unable to control by users ), and for these values, placeholders are added like curly({}) braces inside the text and then these values are run through String format().
  • This function reads the type of arguments passed to it and formatted it according to the format codes that are defined in the string.

Python String **format()**: Syntax

Below we have a basic syntax of String format() in Python:

{}.format(value)

Note: In the above syntax, the value parameter can be an integer,floating-point numeric constant, can be string, character, or even a variable.

Python String **format():**Parameters

This method can take any number of parameters, But parameters are categorized into two types:

  • Positional Parameters:

Positional Parameters consist of a list of parameters that can be accessed with an index of the parameter inside curly braces like this {index}.

  • Keyword Parameters:

Keyword Parameters is a list of parameters which is of type key=value and that can be accessed with a key of the parameter inside curly braces like {key}

Python String **format()**: Returned Values

This String format() returns the formatted string with the value passed as a parameter in the placeholder position.

Python String **format():**Basic Example

Below we have an example to show the working of String format() function:

name = 'Kunj'
coins = 21
print('Hello {}, you have {} coins.'.format(name, coins))

In the above example, curly braces get replaced by name and coins. The output for the same is given below:

Hello kunj, you have 21 coins.

Note: We can use multiple pairs of curly braces while formatting the string. If there is another variable substitution that is needed in a large string then this can be done by just adding the second pair of curly braces and passing the second value into the method. and the Python will replace the placeholders with values in an ordered manner. In the above example, we have already seen this.

Python String **format()**: Formatting of Numbers

There is an example given below where the formatting of numbers can be done. Let us see the code snippet:

a = 1000
print('I have {:d} rupees.'.format(a))

The output will be:

I have 1000 rupees.

Note: In the above code d in {:d} indicates that it is a decimal integer.

Some Format specifiers used for Number are given below

TypeMeaning
dDecimal integer
ccorresponding Unicode character
bbinary format
ooctal format
xRepresent hexadecimal format in lowercase
XRepresent hexadecimal format in uppercase
nSimilar to 'd' but there is a difference it uses the current locale setting for number separator
eExponential notation in lowercase
EExponential notation in uppercase
fdisplay fixed-point number (default value=6)
Fsame as 'f' except it displays 'nan' as 'NAN" and 'inf' as 'INF'
gGeneral format. It Rounds up the number to p significant digits. (Default precision: 6)
GIt is same as ‘g.’ Except for switches to ‘E’ if any number is significant.
%Means Percentage. Multiples by 100 and puts % at the end.