Python Tutorial: How to use format() - Builtin Function in Python

Published: 01 June 2020
on channel: Python Gurgaon
54
1

Python Tutorial: How to use format() - Builtin Function in Python

How to use the format function of python.
The format function can be used to format a string before we decide to print it out to the user. It takes two arguments – a number and a formatting pattern, which is also expressed as a string. The format function returns a string which can be treated like any other string which means we can print it out immediately or store its value in a variable, etc.

The first argument passed to the format function is the variable or value that we wish to format. The second argument passed to the function is the formatting "pattern" we wish to apply to this item. This pattern varies based on what we would like to do to the value in question. Once the pattern is applied to the value, the format function will return a string version of our item with the formatting pattern applied to it.
This format function can be used to generate a customized string version of a float or integer number.

For example, a program using format that outputs a floating point number to two decimal places.

The formatting pattern '.2f' means that we are formatting a floating point value which is indicated by the 'f' character. We want to limit the number of decimal places to 2 as is indicated by the '.2' before the 'f' character.

Notice in all our examples that the format function always returns a string. This means that we can not use the result of the format function directly in a math expression. However, we could use a data type conversion function ( int() or float() ) to turn the string generated by the format() function back into a numeric data type if we wanted to.

Another common use of the format function is to generate a string that contains a known number of characters and/or white spaces. The less than or the opening angle bracket character in the formatting pattern tells the Python interpreter to left justify the string and place the extra spaces, if any, at the end of the new string. The greater than or the closing angle bracket character in the formatting pattern tells the Python interpreter to right justify the string and place the extra spaces, if any, at the beginning of the new string.

Another example is, say we have few strings "Language", "Learning", "Python" and "Computer Programming" and we might want to generate an output that looks like this

Language Learning
Python Computer Programming

In this case we need to ensure that the strings "Language" and "Python" are of the same width so that the strings that come after them ("Learning" and "Computer Programming") line up correctly. We can use the format function to "pad" a string with extra spaces at the beginning or the end of the string. The formatting pattern here gives us the flexibility to add spaces to our string so that we can format the final output to our satisfaction.

Step by step process with examples

Practice by writing the code yourself.

Python Tutorial For Beginners    • Python Tutorial for Beginners  

Python New Releases And Latest Features    • Python New Releases And Latest Features  

#Python #Tutorials #Gurgaon
#format #functions #builtin #formatting #patterns #floats #strings #integers #decimals #points

#Python3 #Gurgaon #Coding #Classes #Gurugram #Programming #Beginner #Tutorial