Python str format with

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:

If you want to contribute more examples, feel free to create a pull-request on Github!

Table of Contents:

  1. Basic formatting
  2. Value conversion
  3. Padding and aligning strings
  4. Truncating long strings
  5. Combining truncating and padding
  6. Numbers
  7. Padding numbers
  8. Signed numbers
  9. Named placeholders
  10. Getitem and Getattr
  11. Datetime
  12. Parametrized formats
  13. Custom objects

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

Источник

Читайте также:  Inline svg css background
Оцените статью