

No argument is converted, results in a " %" character in the result. String (converts any python object using str()). String (converts any python object using repr()). Single character (accepts integer or single character string). Same as " E" if exponent is greater than -4 or less than precision, " F" otherwise. Same as " e" if exponent is greater than -4 or less than precision, " f" otherwise. signed integer decimal.įloating point exponential format (lowercase).įloating point exponential format (uppercase). In the following picture you can see, how it looks, if five float numbers are printed with the placeholder "%6.2f" in subsequent lines: The advantage or beauty of a formatted output can only be seen, if more than one line is printed with the same pattern. Where is the difference? It's easy: There is no difference between "d" and "i" both are used for formatting integers. You may have expected a "%5i" instead of "%5d". As 453 consists only of 3 digits, the output is padded with 2 leading blanks. The number will be printed with 5 characters. The first placeholder "%5d" is used for the first component of our tuple, i.e. Furthermore, the number has been preceded in the output with 3 leading blanks. If you look at the output, you will notice that the 3 decimal digits have been rounded. Finally, the last character "f" of our placeholder stands for "float". the number following the "." in our placeholder. The decimal part of the number or the precision is set to 2, i.e. Our float number 59.058 has to be formatted with 8 characters. This number includes the decimal point and all the digits, i.e. This is followed by the total number of digits the string should contain. Like other placeholders, it is introduced with the "%" character. The second one "%8.2f" is a format description for a float number. Let's have a look at the placeholders in our example.

The general syntax for a format placeholder is %type There are two of those in our example: "%5d" and "%8.2f". The values can be literals, variables or arbitrary arithmetic expressions. On the left side of the "string modulo operator" there is the so-called format string and on the right side is a tuple with the content, which is interpolated in the format string. The following diagram depicts how the string modulo operator works: However, it is very likely that one day this old style of formatting will be removed from the language. You should be capable of understanding it, when you encounter it in some Python code. That's why we cover it in great detail in this tutorial. Unfortunately, string modulo "%" is still available in Python3 and what is even worse, it is still widely used. Since Python 2.6 has been introduced, the string method format should be used instead of this old-style formatting. But it can also be used, for example, to create the right format to put the data into a database. In many cases the string created via the string interpolation mechanism is used for outputting values in a special way. Another term for it is "string interpolation", because it interpolates various class types (like int, float and so on) into a formatted string.

Therefore, it is often called string modulo (or somethimes even called modulus) operator, though it has not a lot in common with the actual modulo calculation on numbers. To this purpose the modulo operator "%" is overloaded by the string class to perform string formatting. So there is a "printf" in Python? No, but the functionality of the "ancient" printf is contained in Python. One can go as far as to say that this answer is not true. To answer "Python has a print function and no printf function" is only one side of the coin or half of the truth. Is there a printf in Python? A burning question for Python newbies coming from C, Perl, Bash or other programming languages who have this statement or function.

Enjoying this page? We offer live Python training courses covering the content of this site.Įnrol here The Old Way or the non-existing printf and sprintf
