# How to format a string in Python

Created
Modified

## Fancier Output Formatting

To use formatted string literals, begin a string with f or F before the opening quotation mark or triple quotation mark. Inside this string, you can write a Python expression between { and } characters that can refer to variables or literal values.

#!/usr/bin/env python3

# Import math module
import math

print(f'The value of pi is approximately {math.pi:5.3f}')

weather = {"Tue":78, "Wed": 60, "Thu": 60}
for name, data in weather.items():
print(f'{name:3} => {data:2d}')
The value of pi is approximately 3.142

Tue => 78
Wed => 60
Thu => 60

## The String format() Method

Basic usage of the str.format() method looks like this:

#!/usr/bin/env python3

# Import math module
import math

print('The value of pi is approximately {:07.3f}'.format(math.pi))

print('{1} and {0}'.format('spam', 'eggs'))
The value of pi is approximately 003.142

eggs and spam

Using type-specific formatting:

#!/usr/bin/env python3

# Import datetime module
from datetime import datetime

curr = datetime.now()
print('{:%Y-%m-%d %H:%M:%S}'.format(curr))
# 2022-04-11 12:01:54

# Using the comma as a thousands separator:
print('{:,}'.format(1234567890))
# 1,234,567,890
2022-04-11 12:01:54

1,234,567,890

## Old String Formatting with % Operator

The % operator (modulo) can also be used for string formatting. Given 'string' % values, instances of % in string are replaced with zero or more elements of values. This operation is commonly known as string interpolation. For example:

#!/usr/bin/env python3

# Import math module
import math

print('PI :: %5.3f' % math.pi)

print('PI :: %007.3f' % math.pi)
PI :: 3.142
PI :: 003.142

The conversion flag characters are:

flag characters:
'#'
The value conversion will use the “alternate form” (where defined below).
'0'
The conversion will be zero padded for numeric values.
'-'
The converted value is left adjusted (overrides the '0' conversion if both are given).
' '
(a space) A blank should be left before a positive number (or empty string) produced by a signed conversion.
'+'
A sign character ('+' or '-') will precede the conversion (overrides a “space” flag).

A length modifier (h, l, or L) may be present, but is ignored as it is not necessary for Python – so e.g. %ld is identical to %d.

The conversion types are:

types:
'd'
Signed integer decimal.
'i'
Signed integer decimal.
'o'
Signed octal value.
'u'
Obsolete type – it is identical to 'd'.
'x'
'X'
'e'
Floating point exponential format (lowercase).
'E'
Floating point exponential format (uppercase).
'f'
Floating point decimal format.
'F'
Floating point decimal format.
'g'
Floating point format. Uses lowercase exponential format if exponent is less than -4 or not less than precision, decimal format otherwise.
'G'
Floating point format. Uses uppercase exponential format if exponent is less than -4 or not less than precision, decimal format otherwise.
'c'
Single character (accepts integer or single character string).
'r'
String (converts any Python object using repr()).
's'
String (converts any Python object using str()).
'a'
String (converts any Python object using ascii()).
'%'
No argument is converted, results in a '%' character in the result.