Hit enter after type your search item

How to print formatted string in Python?

Python has a basic way for formatting strings just like in other programming languages. The ‘%’ or modulus operator (percentage sign) is generally supported in many languages and so in Python.

Besides, Python also has other ways for string formatting. In this tutorial, we will look at ‘%’ operator and string.format() function for string formatting in Python in this tutorial.

First Way: Using ‘%’ for string formatting

The basic string formatting can be done by using the ‘%’ operator. This is an old style that you might be familiar with if you have background with C language. Python also supports this but you may want using the newly available method for string formatting (explained in next section).

The example below shows how ‘%’ operator works:

The output:

The first name ss Mike and second name is Anthony

In the code, you can see I used %s that represents a value as a string. This is followed by using variable name in parenthesis in the same sequence as you would like to replace the %s with string variable values.

You may use various directives for formatting:

  • %s is used for strings
  • %d for decimal integers
  • %i for integers
  • %f for float
  • %x for Hexadecimal integers
  • %o for octal integer
  • %u for unsigned integer
  • %e for float exponent

Using integer and float along with string objects example

In this example, I also used %d for integer and %f for a float along with %s for string representation. For that, I used three object variables that hold Name, age and salary of an employee (for the demo only):

The output of the above code is:

Employee Name is Mike, age is 35 and salary is 5000.00!

Using a list example

You may also use a list as shown in the example below:

The result:

Employee Name is Mike. The paid amount are: [4000, 4000, 4500, 4500, 5000]

Second way: Using string string.format method

The second and more usable way of formatting strings in Python is the str.format function which is part of the string class. The general syntax for using the str.format function is:

format(value[, format_spec])

In this way of string formatting, we use placeholders in the string object. The placeholder is in the form of curly braces i.e. {}.

First, have a look at this simple example of the str.format method and I will explain later how it works:

The output:

Python string format

In the example above, you can see the string formatting is done in two ways. First, a value is given in the format function. The other print function uses a variable for substitution of the place-holder.

An example of using multiple pairs of curly braces

You may also use multiple placeholders for formatting strings i.e. pairs of curly braces.

The result:

Demo with multiple place-holders.

So, you may use more than one value or variables in the format function that are separated by a comma. The substitution is done in the same order as per place-holder in the calling string, however, you may also provide a position as shown in the example below.

The example of using positional arguments

The demo below shows how to use positional arguments in the string format function. In the place-holder, i.e. within the curly braces, you may provide the number according to the argument in the format function.

See an example below and I will explain more:

The output:

Employee Name is Mike, Salary is 4500.

Employee Name is Sarah, Salary is 5000.

A few points to learn from the above example:

  • We used values and object variables in the format function as arguments.
  • In the first print function, I used 0 and 1 and the result is displayed in the same order as I sent arguments. That is, Mike and 4500 in the output formatted string.
  • In the second print function, the order is 1 and 0. The first argument in the format function is emp_sal which is equal to 0 and emp_name = 1. So, in the output string, the emp_name variable value is replaced with {1} and salary by {0}.
  • In that way, you may control the usage of argument position in the formatted string.

Using keyword argument example

Not only you may provide the numbers in the curly braces but you may refer the arguments by keywords as well – thus making code cleaner. Have a look at the code and output below:

The output:

Employee Name is Michelle, Salary is 6500.

Employee Name is Haynes, Salary is 7500.

There, you can see I used {emp} and {sal} to refer these keywords in the format function. Inside the format function, I used values as well as variables with keywords.

You may also notice, the order is not important as calling in the placeholder than the sequence of the format function arguments.

An example of formatting date with string.format

You may format numbers like integers, float etc. as using the string.format function. I will show you in the examples below; first, have a look at using various date directives for formatting dates using format function.

The output as I executed this code:

string format date

In the example, I used datetime module and get the current date by datetime’s today() function. This value is used in the format function with various date directives for formatting.

You may learn more about date formatting options in its tutorial: Python date formatting

How to perform formatting for numbers using format function?

By using format specifiers for numbers, you may also format numbers by using the format() function. A list of specifiers used with % operator is given in the above section. As using the format() function, the specifier is used without ‘%’ operator.

A few commonly used number specifiers/conversion characters are:

  • d is used for decimal integers
  • b for binary format
  • o for octal format
  • x is for the hexadecimal format (lowercase letters)
  • X for hexadecimal (uppercase letters)
  • f for floating point number which is fixed. The default is 6 precision.
  • c for character
  • e is for exponent notation

The syntax for using a specifier is in the format function is as follows:

String {field_name:specifier} str.format(value/variable)

An example of int and float conversion

This example uses int and float numbers in the place-holder:

The output:

Using decimal number = 5000

Float Number = 5000.000000

A float with 2 precision

In above example, you can see the float number is converted with 6 precision which is the default value. For changing the precision, you may do this as follows:

The result:

Float Number with 2 precision = 5555.48

Float Number with 3 precision = 5555.785


This div height required for enabling the sticky sidebar