Tutorial by Examples

foo = 1 bar = 'bar' baz = 3.14 You can use str.format to format output. Bracket pairs are replaced with arguments in the order in which the arguments are passed: print('{}, {} and {}'.format(foo, bar, baz)) # Out: "1, bar and 3.14" Indexes can also be specified inside the bracket...
Python 2.x2.6 The format() method can be used to change the alignment of the string. You have to do it with a format expression of the form :[fill_char][align_operator][width] where align_operator is one of: < forces the field to be left-aligned within width. > forces the field to be righ...
Literal format strings were introduced in PEP 498 (Python3.6 and upwards), allowing you to prepend f to the beginning of a string literal to effectively apply .format to it with all variables in the current scope. >>> foo = 'bar' >>> f'Foo is {foo}' 'Foo is bar' This works wi...
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: >>> from datetime import datetime &...
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' la...
>>> '{0:.0f}'.format(42.12345) '42' >>> '{0:.1f}'.format(42.12345) '42.1' >>> '{0:.3f}'.format(42.12345) '42.123' >>> '{0:.5f}'.format(42.12345) '42.12345' >>> '{0:.7f}'.format(42.12345) '42.1234500' Same hold for other way of referenc...
The .format() method can interpret a number in different formats, such as: >>> '{:c}'.format(65) # Unicode character 'A' >>> '{:d}'.format(0x0a) # base 10 '10' >>> '{:n}'.format(0x0a) # base 10 using current locale for separators '10' Format integers to d...
Note: Everything below applies to the str.format method, as well as the format 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 th...
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) '.......
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...
Format strings may contain named placeholders that are interpolated using keyword arguments to format. Using a dictionary (Python 2.x) >>> data = {'first': 'Hodor', 'last': 'Hodor!'} >>> '{first} {last}'.format(**data) 'Hodor Hodor!' Using a dictionary (Python 3.2+) >&gt...

Page 1 of 1