Formatting Output in Python using % and { }

Python print() function is used to display our output on the screen. It is almost with all the python versions with little difference. The print () function that shipped with 3.x is a little bit different and has the following syntax

print(value, separator, end)

few examples of the above is as follows

print(10)
print(10,20)
print(10,20,”-“)
print(“10”,20,30,”-----“, end=””)

When separator and end are not defined than python assumes space as a separator between two values and “\n” ie new line as the end of that print function.

This is the simple approach that we people are used to see in most of the python programs. But there are two more methods % and  {} using them we can format our output in a better way.

% method to format our output comes from C language where each and everything was printed based on the type of data a variable holds. Thus here in python, it has some special chars that are associated with % symbol

%d  and %g         stand for an integer value

%s stands for string value – it is used with a number of  chars you want to display

%f                           stand for float value.

So if you have the following values in your program like this

Name  = ‘Rakesh Kumar’
Age  =  25
Salary  = 15000.35

Then you can print the same information using the following ways

1st Method

Print(‘ My name is:’, name,’ my age is:’, age , ‘ and my salary is:’, salary )

2nd Method

Print(‘ My name is:’, name, end=” ”)
Print(my age is:’, age , end =” “)
Print( ‘ and my salary is:’, salary )

3rd Method

print ( “My Name is: %s   my age is: %d  and my salary is: %.2f”  % ( name, age, salary))

Remember the position of  %s,  %d and  %f and the sequence of variable, if you miss the position then the program is going to produce an error.

4th Method

print ( My Name is  : {}   my age is : {}  and my salary is  : {}” .format ( name, age, salary))

In this method the value of variable matter the most. This method is developed using a string method format( ). The first value takes place in the first position and the second value will automatically take the position and so on.

Thus if you place salary at first position and name at the second position then you are not going to see any error message at all.

5th Method

print ( My Name is  : {0}   my age is : {1}  and my salary is  : {2}” .format ( name, age, salary))

This is the exact replica of method 4 except it contains the index of the values. Change the index and the values automatically change its position.

The output of the above statement would be like this

My Name is: Rakesh and my age is: 25 and my salary is 10000.35

I hope you will find these formating methods useful and when you have lots of information to display on the screen like CSV file results using file handling in Python then these formatting output methods have immense power to beautify outputs.

Related Posts

If you like CBSEToaday and would like to contribute, you can also write an article using submit article or mail your article to contribute@cbsetoday.com See your article appearing on the cbsetoday.com main page and help other students/teachers.