Was anyone else smiling uncontrollably and nodding their head at the monitor?
Announcement
Collapse
No announcement yet.
Spurs (A): Build Up
Collapse
X
-
On from, Ings had to play, but if Klopp was planning on plying one up top anyway, I quite like the idea of Lallana and Coutinho playing wide of Sturridge.Originally posted by foresterbloke View PostWe're missing Ings and Gomez but they're arguably substitutes rather than first XI.If we are all only happy when we are really winning in the end, when your race finishes, what life would that be?
Comment
-
Ere' you go.......Originally posted by Lee View PostBendy!
Formatting. Please.
Basic formatting
Simple positional formatting is probably the most common use-case. Use it if the order of your arguments is not likely to change and you only have very few elements you want to concatenate.
Since the elements are not represented by something as descriptive as a name this simple style should only be used to format a relatively small number of elements.
Old
'%s %s' % ('one', 'two')
New
'{} {}'.format('one', 'two')
Output
one two
Old
'%d %d' % (1, 2)
New
'{} {}'.format(1, 2)
Output
1 2
With new style formatting it is possible (and in Python 2.6 even mandatory) to give placeholders an explicit positional index.
This allows for re-arranging the order of display without changing the arguments.
This operation is not available with old-style formatting.
New
'{1} {0}'.format('one', 'two')
Output
two one
Value conversion
The new-style simple formatter calls by default the __format__() method of an object for its representation. If you just want to render the output of str(...) or repr(...) you can use the !s or !r conversion flags.
In %-style you usually use %s for the string representation but there is %r for a repr(...) conversion.
Setup
class Data(object):
def __str__(self):
return 'str'
def __repr__(self):
return 'repr'
Old
'%s %r' % (Data(), Data())
New
'{0!s} {0!r}'.format(Data())
Output
str repr
In Python 3 there exists an additional conversion flag that uses the output of repr(...) but uses ascii(...) instead.
This operation is not available with old-style formatting.
Setup
class Data(object):
def __repr__(self):
return 'räpr'
New
'{0!r} {0!a}'.format(Data())
Output
räpr r\xe4pr
Padding and aligning strings
By default values are formatted to take up only as many characters as needed to represent the content. It is however also possible to define that a value should be padded to a specific length.
Unfortunately the default alignment differs between old and new style formatting. The old style defaults to right aligned while for new style it's left.
Align right:
Old
'%10s' % ('test',)
New
'{:>10}'.format('test')
Output
test
Align left:
Old
'%-10s' % ('test',)
New
'{:10}'.format('test')
Output
test
By argument:
In the previous example, the value '10' is encoded as part of the format string. However, it is possible to also supply such values as an argument.
Old
'%*s' % ((- 8), 'test')
New
'{:<{}s}'.format('test', 8)
Output
test
Again, new style formatting surpasses the old variant by providing more control over how values are padded and aligned.
You are able to choose the padding character:
This operation is not available with old-style formatting.
New
'{:_<10}'.format('test')
Output
test______
And also center align values:
This operation is not available with old-style formatting.
New
'{:^10}'.format('test')
Output
test
Truncating long strings
Inverse to padding it is also possible to truncate overly long values to a specific number of characters.
The number behind a . in the format specifies the precision of the output. For strings that means that the output is truncated to the specified length. In our example this would be 5 characters.
Old
'%.5s' % ('xylophone',)
New
'{:.5}'.format('xylophone')
Output
xylop
By argument:
Old
'%.*s' % (7, 'xylophone')
New
'{:.{}}'.format('xylophone', 7)
Output
xylopho
Combining truncating and padding
It is also possible to combine truncating and padding:
Old
'%-10.5s' % ('xylophone',)
New
'{:10.5}'.format('xylophone')
Output
xylop
Numbers
Of course it is also possible to format numbers.
Comment
Comment