Basics of String Formatting
Given the variables:
foo = 1
bar = 'bar'
baz = 3.14
You can use str.format
to format your output. The bracket pairs are matched to the arguments passed to format in the exact same order.
print('{}, {} and {}'.format(foo, bar, baz))
# Out: "1, bar and 3.14"
You can also specify an index inside the brackets. The number inside the brackets corresponds to the indexing of the arguments you pass to the str.format
function (0-based).
print('{0}, {1}, {2}, and {1}'.format(foo, bar, baz))
# Out: "1, bar, 3.14, and bar"
You can also reference the fields of any objects you pass into str.format
:
my_dict = {'key': 6}
print('{0.key}'.format(my_dict))
# Out: "6"
You can also use named arguments.
print("X value is: {x_val}. Y value is: {y_val}.".format(x_val=2, y_val=3))
# Out: "X value is: 2. Y value is: 3."
Note: In some Python 2 code samples available online, you may notice the use of
%
for formatting strings instead of thestr.format
function. The%
is not recommended for use anymore and will be deprecated later. Hence, you should always strive to use the newer way to format strings via thestr.format
function.
Alignment
The format()
method can be used to change the alignment of the string. You have to do it with the following operators:
<
forces the field to be left-aligned within the available space.>
forces the field to be right-aligned within the available space.^
forces the field to be centered within the available space.=
forces the padding to be placed after the sign (numeric types only).
print("{:<9s}, World".format("Hello"))
# Out: "Hello , World"
print("{:>9s}, World".format("Hello"))
# Out: " Hello, World"
print("{:^9s}".format("Hello"))
# Out: " Hello "
print("{:0=6d}".format(-123))
# Out: "-00123"
Format literals
Python 3.6 introduces literal format strings, where prepending f
to the beginning of a string literal effectively applies .format
to it with all variables in the current scope.
foo = "bar"
print(f"Foo is {foo}")
# Out: "Foo is bar"
This works with more advanced formats too, including alignment and dot notation.
print(f"{foo:^7s}")
# Out: " bar "
Integer types
print("{:b}".format(10)) # (base 2)
# Out: "1010"
print("{:c}".format(65)) # (Unicode character)
# Out: "A"
print("{:d}".format(0x0a)) # (Base 10)
# Out: "10"
print("{:o}".format(10)) # (Base 8)
# Out: "12"
print("{:x}".format(10)) # (Lowercase Base 16)
# Out: "a"
print("{:X}".format(10)) # (Uppercase Base 16)
# Out: "A"
print("{:n}".format(0x0a)) # (Base 10 using current locale for separators)
# Out: "10"
Float formatting
print("{0:.0f}".format(42.12345))
# Out: 42
print("{0:.1f}".format(42.12345))
# Out: 42.1
print("{0:.3f}".format(42.12345))
# Out: 42.123
print("{0:.5f}".format(42.12345))
# Out: 42.12345
print("{0:.7f}".format(42.12345))
# Out: 42.1234500
Same hold for other way of referencing:
print("{:.3f}".format(42.12345))
# Out: 42.123
print("{answer:.3f}".format(answer=42.12345))
# Out: 42.123
Floating point numbers can also be formatted in scientific notation or as percentages:
print("{0:.3e}".format(42.12345))
# Out: 4.212e+01
print("{0:.0%}".format(42.12345))
# Out: 4212%
String formatting with datetime
Any class can configure its own string formatting syntax through the __format__
method. A type in the standard Python library that makes handy use of this is the datetime
type, where one can use strftime
-like formatting codes directly within str.format
:
import datetime
print("North-America: {dt:%m/%d/%Y}. Others: {dt:%d/%m/%Y}. Best: {dt:%Y-%m-%d}".format(dt=datetime.datetime.now()))
# Out: North-America: 07/21/2016. Others: 21/07/2016. Best: 2016-07-21
A full list of list of datetime formatters can be found in the python docs.
Character to ASCII Code and back
If you want the ASCII code for the character a
, you would do something like:
ord('a')
# 97
To convert an ASCII code back to a character, you would do something like:
chr(97)
# 'a'
The ord(...)
only accepts a character or a string with a single character. If you try passing 'abc'
as parameter then you would get the following error:
ord('abc')
Traceback (most recent call last):
ord('abc')
TypeError: ord() expected a character, but string of length 3 found
Custom formatting for a class
Note:
Everything below applies to the
str.format
method, as well as theformat
function. In the text below, the two are interchangeable.
For every value which is passed to the format
function, Python looks for a __format__
method for that argument. Your own custom class can therefore have their own __format__
method to determine how the format
function will display and format your class and it's attributes.
This is different than the __str__
method, as in the __format__
method you can take into account the formatting language, including alignment, field width etc, and even (if you wish) implement your own format specifiers, and your own formatting language extensions.1
object.__format__(self, format_spec)
For example :
# Example in Python 2 - but can be easily applied to Python 3
class Example(object):
def __init__(self,a,b,c):
self.a, self.b, self.c = a,b,c
def __format__(self, format_spec):
""" Implement special semantics for the 's' format specifier """
# Reject anything that isn't an s
if format_spec[-1] != 's':
raise ValueError('{} format specifier not understood for this object', format_spec[:-1])
# Output in this example will be (<a>,<b>,<c>)
raw = "(" + ",".join([str(self.a), str(self.b), str(self.c)]) + ")"
# Honor the format language by using the inbuilt string format
# Since we know the original format_spec ends in an 's'
# we can take advantage of the str.format method with a
# string argument we constructed above
return "{r:{f}}".format( r=raw, f=format_spec )
inst = Example(1,2,3)
print "{0:>20s}".format( inst )
# out : (1,2,3)
# Note how the right align and field width of 20 has been honored.
Note:
If your custom class does not have a custom
__format__
method and an instance of the class is passed to theformat
function, Python2 will always use the return value of the__str__
method or__repr__
method to determine what to print (and if neither exist then the defaultrepr
will be used), and you will need to use thes
format specifier to format this. With Python3, to pass your custom class to theformat
function, you will need define__format__
method on your custom class.
Format using Getitem and Getattr
Any data structure that supports __getitem__
can have their nested structure formatted:
person = {'first': 'Arthur', 'last': 'Dent'}
'{p[first]} {p[last]}'.format(p=person)
# 'Arthur Dent'
Object attributes can be accessed using getattr()
:
class Person(object):
first = 'Zaphod'
last = 'Beeblebrox'
'{p.first} {p.last}'.format(p=Person())
# 'Zaphod Beeblebrox'
Hex formatting
An easy way to convert a rgb tuple (ranged from 0 to 1 for each, as is the standard for colorsys
to a hex string (for example for RBG values) is
"0x{:02x}{:02x}{:02x}".format(int(255 * rgb[0]), int(255 * rgb[1]), int(255 * rgb[2]))
For example, the tuple for pure red rgb = (1, 0, 0)
would be formatted as '0xff0000'
Named placeholders
Nested formatting
Some formats can take additional parameters, such as the width of the formatted string, or the alignment:
"{:.>10}".format("foo")
# '.......foo'
Those can also be provided as parameters to format
by nesting more {}
inside the {}
:
"{:.>{}}".format("foo", 10)
# '.......foo'
"{:{}{}{}}".format("foo", "*", "^", 15)
# '******foo******'
In the latter example, the format string "{:{}{}{}}"
is modified to "{:*^15}"
(i.e. "center and pad with * to total length of 15") before applying it to the actual string "foo"
to be formatted in that way.
This can be useful in case those parameters are not known beforehand, e.g. for aligning tabular data:
data = ["a", "bbbbbbb", "ccc"]
m = max(map(len, data))
for d in data:
print("{:>{}}".format(d, m))
# Out:
# a
# bbbbbbb
# ccc
Padding and truncating strings, combined
Say you want to print variables in a 3 character column.
Note: doubling {
and }
escapes them.
s = """
pad
{{:3}} :{a:3}:
truncate
{{:.3}} :{e:.3}:
combined
{{:>3.3}} :{a:>3.3}:
{{:3.3}} :{a:3.3}:
{{:3.3}} :{c:3.3}:
{{:3.3}} :{e:3.3}:
"""
print (s.format(a="1"*1, c="3"*3, e="5"*5))
Output:
pad
{:3} :1 :
truncate
{:.3} :555:
combined
{:>3.3} : 1:
{:3.3} :1 :
{:3.3} :333:
{:3.3} :555:
String slicing
Python strings can be sliced just like lists to generate a substring from the original string. The arguments for slicing are [start:stop:step]
, where start
is the index of the first letter in the new substring, stop
is the last index (not inclusive), and step
is the interval between each step.
Example:
test = "string"
test[:2]
# Out: 'st'
test[2:4]
# Out: 'ri'
test[4:]
# Out: 'ng'
test[6:]
# Out: ''
test[1:5:2]
# Out: 'ti'
test[::-1]
# Out: 'gnirts' (the string is reversed)
Notes:
- If the
start
index is not provided, it defaults to 0 - If the
stop
index is not provided, it defaults tolen(string)
- If the
step
index is not provided, it defaults to 1 - Zero, one, two, or all three arguments can be used (
str[:]
,str[1:]
,str[::2]
,str[1::2]
,str[1:10:3]
are all valid)
Join operator
Join takes a list of strings and joins them together with the calling string in between each element.
To add spaces between words in a list:
>>> ' '.join(['stack', 'overflow', 'rocks'])
'stack overflow rocks'
To join without spaces
>>> music = ["Abba","Rolling Stones","Black Sabbath","Metallica"]
#Join a list with an empty space
>>> print(''.join(music))
AbbaRolling StonesBlack SabbathMetallica
String Concatenation
Concatenation combines two (or more) strings into a new string object.
With '+' operator
print("You can concatenate two " + "strings with the '+' operator.")
# Out: You can concatenate two strings with the '+' operator.
You may not concatenate an integer with a string:
print 'red' + 3
# Out: Traceback (most recent call last):
# File "", line 1, in
# TypeError: cannot concatenate 'str' and 'int' objects
But you can turn a number into a string if you use the str()
function.
print 'red' + str(3)
# Out: red3
With '*' to repeat string
print 'red' * 3
# Out: redredred