How to Format String in Python

Python String Format() Function Tutorial With Example

Python string format() is a built-in function that formats a given string into the more excellent output. The String.format() method is one of the string formatting methods in Python3, which allows multiple substitutions and value formatting.

The format() method lets us concatenate elements within the string through positional formatting.

Python String Format

To format a string in Python, use the string format() function. The String format() method takes any number of parameters and returns the formatted string. The format() reads the type of arguments passed to it and formats it according to the format codes defined in the string.

  1. Positional parameters – It is the list of parameters that can be accessed with an index of the parameter inside curly braces {index}.
  2. Keyword parameters – It is the list of parameters of type key=value that can be accessed with a key of the parameter inside curly braces {key}.

The format() method 

See the following syntax.


The value parameter can be an integer, floating-point numeric constant, string, characters or even variables.

It will return the formatted string with the value passed as a parameter in the placeholder position.

Basic formatting with a format() method

Let’s see the basic formatting in action. Write the below code inside the file.


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

In the above code, The {} curly braces are replaced by name and coins variables. 

See the output.

Basic formatting with a format() method

You can also write the above code like the below code.


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

See the output.

Python String Format Tutorial With Example

Python String Multiple Formatters

The multiple pairs of curly braces can be used while formatting the string. Let’s say if another variable substitution is needed in a large string, it can be done by adding the second pair of curly braces and passing the second value into the method.

Python will replace the placeholders with values in order. We have already seen the example of Multiple String Formatters above.

Python Number Formatting

Let’s see the simple string number formatting in Python. Write the following code.


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

See the output.

Python Simple Number Formatting

Let’s take float, binary and hexadecimal datatype example.


print('The float number is:{:f}'.format(21.19219219219))

print('bin: {0:b}, oct: {0:o}, hex: {0:x}'.format(1921))

See the output.

Python Number Formatting

You can format the numbers using a format specifier given below.

Type Meaning
d Decimal integer.
c Corresponding Unicode character.
b Binary format.
o Octal format.
x The hexadecimal format in lower case.
X The hexadecimal format in the upper case.
n Same as ‘d’. Except it uses the current locale setting for number separator.
e Exponential notation. (lowercase e)
E Exponential notation (uppercase E)
f Displays fixed-point number (Default: 6)
F Same as ‘f.’ Except for 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 for switches to ‘E’ if the number is significant.
% Percentage. Multiples by 100 and puts % at the end.


That’s it for this tutorial.

Leave A Reply

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.