Output Formatting in Python
Output of a program can be presented in several ways. Data can be printed in a readable form or may be written to a file for future use.
User can handle string by using string slicing and concatenation operations to create any layout. The string has some methods that can perform useful operations for padding strings to a given column width.
If you have an idea to develop a new application, you can click on mister saad
Output Formatting using String modulo operator (%):
The String formatting can also be done by using % operator. It interprets the left argument like printf() format string which is applied to right argument. In python, we don’t have printf() function. For this purpose, modulo operator is overloaded by string class in order to perform string formatting. Hence it is often known as String modulo operator.
In the below example, “%2d” is a place holder is used for the first component of the tuple. Due to this, the number will be printed with 2 characters.
For format description of float number, “%5.2f” is used. It is also introduced with character % like other place holders. It is followed by the total number of digits that the string should contain. It includes the decimal point and all digits before and after the decimal point.
The decimal part of the number or the precision is set to 2, the number following “.” In the place holder. The last character “f” in place holder stands for float.
Python Code:
#integer and float value
print("integer : % 2d, float : % 5.2f" %(1, 05.333))
#integer value
print("Total Fruits : % 3d, Mango : % 2d" %(240, 120))
#octal value
print("% 5.3o"% (50))
#exponential value
print("% 10.2E"% (456.1458))
Output:
integer : 1, float : 5.33
Total Fruits : 240, Mango : 120
062
4.56E+02
Output Formatting using Format method:
Formatting output using format method requires more manual effort. User uses {} to mark the substitution of variable and provides detailed formatting directives, but user should provide the information for formatting. This method lets us to concentrate elements within an output through positional formatting.
Brackets and characters within them which are known as format fields are replaced with the objects are passed into format () method. We can use number in the brackets to refer the position of the object passed into format () method.
Python Code:
# using format() method
print('{} and {}'.format('Python', 'Java'))
# using format() method and referring
# a position of the object
print('{0} is {1}'.format('Python', 'Programming Language'))
print('{1} is {0}'.format('Python', 'Programming Language'))
Output:
Python and Java
Python is Programming Language
Programming Language is Python
Python Code:
# format() method with number
print("Mathematics:{0:2d}, Physics :{1:6.3f}". format(80, 75.66))
# varying positional argument
print("Second: {1:3d}, first: {0:3.2f}". format(206.3, 42))
print("Gold: {g:4d}, Silver: {s:5.2f}". format(g = 3500, s = 256.094))
Output:
Mathematics:80, Physics :75.660
Second: 42, first: 206.30
Gold: 3500, Silver: 256.09
Output Formatting using String Method:
We can format the output by using String method by String slicing and concatenations. Through these methods, output can be formatted in exclusive way. Some of the methods are str.rjust(), str.centre() and str.ljust().
str.rjust(): Through this function, we can align the sentence towards right side.
str.centre(): By using this function, sentence can be moved towards the center.
str.ljust(): We can move the sentence towards the left by this string function.
Python Code:
stmt = "Output Formatting Functions"
# Printing the right aligned sentence
print ("Right aligned statement: ")
print (stmt.rjust(50, '*'))
# Printing the center aligned sentence
print ("Center aligned string with : ")
print (stmt.center(60, '#'))
# Printing the left aligned sentence
print ("The left aligned string is : ")
print (stmt.ljust(50, '*'))
Output:
Right aligned statement:
***********************Output Formatting Functions
Center aligned string with :
################Output Formatting Functions#################
The left aligned string is :
Output Formatting Functions***********************