Title
PyFormat: Using % and .format() for great good!
Go Home
Category
Description
Address
Phone Number
+1 609-831-2326 (US) | Message me
Site Icon
PyFormat: Using % and .format() for great good!
Page Views
0
Share
Update Time
2022-05-02 07:16:18

"I love PyFormat: Using % and .format() for great good!"

www.pyformat.info VS www.gqak.com

2022-05-02 07:16:18

Contribute on GitHub PyFormat Using % and .format() for great good! Python has had awesome string formatters for many years but the documentation on them is far too theoretic and technical. With this site we try to show you the most common use-cases covered by the old and new style string formatting API with practical examples. All examples on this page work out of the box with with Python 2.7, 3.2, 3.3, 3.4, and 3.5 without requiring any additional libraries. Further details about these two formatting methods can be found in the official Python documentation: old style new style If you want to contribute more examples, feel free to create a pull-request on Github! Table of Contents: Basic formatting Value conversion Padding and aligning strings Truncating long strings Combining truncating and padding Numbers Padding numbers Signed numbers Named placeholders Getitem and Getattr Datetime Parametrized formats Custom objects Basic formatting Simple positional formatting is probably the most common use-case. Use itif the order of your arguments is not likely to change and you only havevery few elements you want to concatenate.Since the elements are not represented by something as descriptive as aname this simple style should only be used to format a relatively smallnumber 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 thearguments. 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 theoutput of str(...) or repr(...) you can use the !s or !r conversionflags.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 outputof repr(...) but uses ascii(...) instead. Setup class Data(object): def __repr__(self): return 'räpr' Old '%r %a' % (Data(), Data()) 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 asneeded to represent the content. It is however also possible to define thata value should be padded to a specific length.Unfortunately the default alignment differs between old and new styleformatting. The old style defaults to right aligned while for new styleit's left.Align right: Old '%10s' % ('test',) New '{:>10}'.format('test') Output test Align left: Old '%-10s' % ('test',) New '{:10}'.format('test') Output test Again, new style formatting surpasses the old variant by providing morecontrol 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 '{:_