Python Output FormattingIn this tutorial, we will learn to format the output. Formatting and output refer to presenting the output of a program. We can format the output in the human-readable form or write the data into a file and some other specified form. Sometimes we need to control the coming output so we can format it according to our requirements. Users can handle strings by using string slicing and concatenation operations to create any layout. There are various ways to format output.
Formatting Output using String Modulo Operator (%)The % operator helps us to format the string. It interprets the left argument like printf() format string applied to the right argument. Python doesn't provide the printf() function. For this purpose, the modulo operator is overloaded by the string class to perform string formatting. It is also known as the String Modulo operator. The string modulo operator is still available in Python (3.x), and it is widely used, but nowadays, the old formatting style is removed from the language. Let's understand the following example - Example - Output: integer : 1, float : 5.33 Total Player : 24, Batsman : 12 062 4.56E+02 Explanation - In the above code, "%2d" is a placeholder that uses the first element of the tuple. It prints the two characters. For the format description of the float number, "%5.2f" is used. It is also introduced with character % like other placeholders. It shows the total number of strings it can contain. The decimal part of the number or the precision is set to 2, the number following "." in the placeholder. The last character, "f" shows a float number. Formatting Output Using the Format MethodThe format() method takes more manual effort than other methods. We use {} to mark the substitution of variables and provide detailed formatting directives, but we also need to provide the formatted information. We can use the number in the brackets as positional formatting. Let's understand the following example. Example - Output: Hello World Welcome to JavaTpoint JavaTpoint to Welcome The brackets and characters within the item are called format fields which are replaced with the object passed into the format() method. A number in the brackets can be used to refer to the object's position passed into the format() method. Let's understand another example - Example - Output: Number one position is Java, T, and Point. Java :12, Tpoint : 0.55 Second argument: 11, first one: 47.42 Java: 453, Tpoint: 59.06 Example - 3: Output: Java: 4127; For: 4098; Java: 4127 I love JavaTpoint Website Formatting output using the String MethodWe can also format the output using the string slicing and concatenation operations. The string type has some methods that help format output in a fancier way. Few methods which helps to formatting an output - str.ljust(), str.rjust(), and str.centre(). Let's understand the following example - Example - Output: Center aligned string with fillchr: $$$$$$I love JavaTpoint$$$$$$$ The left aligned string is: I love JavaTpoint&&&&&&&&&&&&&&&&&&&&&&& The right aligned string is : -----------------------I love JavaTpoint Format Conversion Rule in PythonBelow is the table of the conversion and its meaning.
ConclusionWe have discussed how to format the output in Python. We have described the various techniques along with examples. Python provides the facility to modify the output according to the requirements. Users can use string or format() methods with positional arguments. This tutorial also included the conversion rules table. Next TopicPython Property Decorator |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India