Python Intermediate: How to use Output Formatting in Python

Output Formatting

There are several rules for presenting the output of the Python programming language. Data can be printed in human-readable form, written to a file for future use, or even in some other specific form. Users often want more control over the output format than just print space-separated values. There are several ways to format the output.

Formatted String Literals

Python 3 introduced a new string formatting mechanism known as Literal String Interpolation or more commonly as F-strings (because of the leading f character preceding the string literal). The idea behind f-strings is to make string interpolation simpler.

Such as,

Example

# Python3 program introducing f-string
 
name = 'Karim'
age = 23
print(f"Hello, My name is {name} and I'm {age} years old.")

Output:

Hello, My name is Karim and I'm 23 years old.

Python format() function

For more effective handling of sophisticated string formatting, Python has included the format() method. The built-in string class’s technique for sophisticated variable substitutions and value formatting offers these features. It is thought that this new formatting method is more elegant. String.format(a, b,…) is the format() method’s default syntax.

Example:

print("Hi ! My name is {} and I am {} years old".format("Karim", 23))
 
print("My Courses are {}, {}, {}, {}".format("Physics", "Chemistry", "Mathematics", "Biology"))

Output:

Hi ! My name is Karim and I am 23 years old
My Courses are Physics, Chemistry, Mathematics, Biology

String modulo operator

String formatting can also be done with the percent operator. It understands the left argument to be applied to the right argument in a manner similar to C language strings with a print-style format. Although Python doesn’t have a print function, it contains the functionality of the old print. The string class performs string formatting for this reason by overloading the modulo operator percent. As a result, it is frequently referred to as a string modulo (or even modulus) operator.

In Python(3.x), the string modulo operator (percent) is still supported and is frequently used. However, the language no longer uses outdated formatting.

Example:

print("Marks : %2d, CGPA : %5.2f" % (79, 3.75))
 
# print integer value
print("Total students : %3d, Teacher : %2d" % (240, 20))
 
# print octal value
print("Octal Value of 67: %7.3o" % (67))
 
# print exponential value
print("Exponential value of 456.9876: %10.3E" % (456.9876))

Output:

Marks : 79, CGPA :  3.75
Total students : 240, Teacher : 20
Octal Value of 67:     103
Exponential value of 456.9876:  4.570E+02

Here are the placeholders from our example.

The first component of our tuple, the number 79, is represented by the first placeholder “%2d.” Two characters will be used to print the number. Since 79 is made up of just one number, leading blanks of 79 are used to pad the output.

The second one, ” %5.2f,” is a float number’s format description. It begins with the % character, just like other placeholders. The entire number of digits the string must contain is then given. The decimal point and all other digits, both before and after the decimal point, are included in this number.

The format for our floating-point number, 3.75, calls for five characters. The accuracy or decimal portion of the number is set to 2.

String method

String splitting and concatenation procedures are used to format this output. There are certain methods available for the string type that aid with output formatting in a more elegant manner. certain techniques for formatting output. Such as str.ljust(), str.rjust(), and str.centre()

Example:

string_value = "My name is khan!"
 
print ("Center aligned string with space: ")
print (string_value.center(40, ' '))
 
print()
print ("The left aligned string is : ")
print (string_value.ljust(40, ' '))
 
print()
print ("The right aligned string is : ")
print (string_value.rjust(40, ' '))

Output:

Center aligned string with space:
           My name is khan!           
 
The left aligned string is :
My name is khan!                       
 
The right aligned string is :
                       My name is khan!

Python end parameter in print()

The print() method in Python always finishes with a new line. How to print without a newline may be a mystery to a programmer with a C/C++ background. The ‘end’ option of the print() function in Python is available. This parameter’s value by default is “\n,” or the new line character. Using this argument, you may terminate a print statement with any character or string.

Example:

print("EnableGeek is" , end = ' ')
print("Awesome", end = ' ')

Output:

EnableGeek is Awesome

There is one more program to show how the end parameter functions.

Example:

print("email" , end = '@')
print("example.com")

Output:

[email protected]
Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit

Check Our Ebook for This Online Course

Advanced topics are covered in this ebook with many practical examples.