Let's introduce again the same previous examples with str.format( ) house = "Gryffondor"
It corrects the errors that were caused by the oldstyle, offers more flexibility, and a better level of readability and it is quicker. This option was provided with Python 2.7, that is why I called it the not-so-new style as it had been some time now since it joined the Python standard features. The Python documentation does not recommend this option and indicates clearly that it exhibits a variety of quirks that lead to a number of common errors such as failing to display tuples and dictionaries correctly.Īlthough this style is error-prone, it is not deprecated with Python 3.x and it is still widely-used because it is simple to type, the logging module uses this style, and, from Python 3.5, the type byte uses this operator for formatting The Not-So-New Style: str.format( ) However, the %-formatting is error-prone, cumbersome, and not very adapted to have good readability in your code. This option offers the ability to convert values using the _str_() and _repr_() methods. #Notice that only the 5 first letters of house were printed Print ' %d points for %.5s !!' % (points, house) Print ' %-10d points for %s !!' % (points, house) Print ' %10d points for %s !!' % (points, house) This style offers a certain degree of flexibility for padding and aligning strings. So for short and simple strings to format, it is very suitable. The advantage of this style is that is easy to use and simple to write. #TypeError: %d format: a number is required, not str Print ' %d points for %d !!' % (points, house) #house is a str however i use %d to format it If you mention the wrong type, a TypeError will be raised (see the example below). All other types are either not supported, or converted to one of these types before formatting. To get started, see the installation instructions, the library reference, and the command line interface. Only ints, strs, and doubles can be formatted. This is a Python implementation of John Gruber’s Markdown.It is almost completely compliant with the reference implementation, though there are a few very minor differences.See John’s Syntax Documentation for the syntax rules. %-formatting is limited as to the types it supports. Print ' %d points for %s !!' % (points, house) Let's have an example: house = "Gryffondor"
This style uses C-style string formatting to create new, formatted strings. This modulo operator is the same one used in C language. This new formatting technique is regarded as more elegant. This method of the built-in string class provides functionality for complex variable substitutions and value formatting.
It is one of the rare contradiction with the Zen of Python that indicates clearly that there must be always one and only one way to do something. Python format () function has been introduced for handling complex string formatting more efficiently. Going through the official Python documentation, you find out that the language offers four options to format strings.