In the Python language, we have a rich collection of built-in methods and functions. These functions are very useful and provide an easy and elegant way for writing codes, one such method is .format python method. It provides functionality to format values in the desired way by mentioning the representation in the format specifier.
The Python format function is very useful and provides a convenient way of number formattings. For instance, it provides an elegant way to convert integers to binary format, to set precision, to set alignment and much more. This article will give a deep and conceptual understanding of all these implementations of format() function along with an example of each.
Definition:
- The python format() method will return a formatted value as specified by the format passed as a parameter.
- The python format() method is an effective way of converting and organizing values. Also, formatting makes the code look more presentable and readable.
Format() Syntax:
The syntax followed by the python .format() function is:
format(value, format)
Format() Python Parameters:
Two parameters are taken by the python .format() function. The first parameter contains the value on which the formatting operation is to be performed and the second parameter specifies the format on how the value has to be formatted.
Return value of format():
The format() method will return a formatted representation of the passed value modified according to the format specified in the format specifier.
Example 1: Number formatting with format()
Example:
# d is a type option and it specifies that the number is an integer
# f is a type option that specifies that the number is a float value
# b is a type option that specifies the binary format
# integer format
print (format (100, “d”))
# floating point numbers
print (format (100.12, “f”))
# binary format
print (format (7, “b”))
Output:
100
100.12
111
Explanation:
The above example returns a formatted representation of the numbers formatted in an integer type, floating-point type and binary type respectively.
Example 2: Number formatting with fill, align, sign, width, precision and type
Example:
# integer number
print (format (24, “#^+5d”))
# float number
print (format (24.27, ">-08.1f"))
Output:
#+24#
000024.3
Explanation:
In the above example, our value is integer “24” and the format specified is “*^+5d”.
For formatting of a floating-point number, our value is “24.27” and the format specified is “>-08.1f”
Let us understand the meaning of each option used in the format specifier
Number formatting with Fill
* : It is a fill character. It formats the value by filling up the empty spaces left after formatting. This fill character can be any number, symbol or alphabet.
Number formatting with Align
^ : It is the center alignment option. It aligns the formatted representation of value to the center of the remaining space.
< : It is the left alignment option. It aligns the formatted representation of value to the left of the remaining space.
> : It is the right alignment option. It aligns the formatted representation of value to the right of the remaining space.
Number formatting with Sign
+ : It is the sign option. It forces the value to have a sign on it’s left.
– : It is the sign option. It forces only the negative value to have a sign on it’s left.
Number formatting with Width
For “*^+5,d”
5 : It is the width option. It formats the number to take a minimum width of 5 and empty spaces are filled by fill character. Like in above example the other spaces are filled with a #.
For “>-08.1f”
8 : It is the width option. It formats the number to take a minimum width of 8 where spaces of the decimal point and signs are included. Like in the above example only the spaces other than values and decimal points are filled with 0.
Number formatting with Precision
.1 : It is the precision operator. It sets the precision of formatted float values. Like in our example it rounded off 24.27 to 24.3 to set the precision to 1.
Number formatting with Type
d : It is the type option. It specifies the value as an integer.
f : It is the type option. It specifies the value as a floating-point number.
Example 3: Using format() by overriding __format__()
Example:
class Student:
def __format__(self, format):
if(format == ‘enrollment_number’):
return ‘32’
return ‘None’
print
(format(Student(), “enrollment_number”))
Output:
32
Explanation:
In the above example, the format() function internally runs Student.__format__(“enrollment_number”) to return 32. By using this we have overridden the __format__() method of Student class. It accepts a format parameter and returns 32 if it is equal to enrollment_number. In case no format is specified, it will return None.
Frequently Asked Questions
Q.1 : What does .format do in Python?
Answer: In python, string .format() method provides the functionality for efficiently handling complex string substitutions and data formatting. It returns the formatted string with the value passed as a parameter in the place of curly braces.
Example:
str = “You are reading an example of {}”
print (str.format (“.format method”))
Output:
You are reading an example of .format method
Q.2 : What is %s and %D in Python?
Answer: The %s and %d are format specifiers used to format strings and numbers respectively.
The %s operator allows you to add a value to a python string and can be used to concatenate multiple strings.
Example:
name = “Meera”
print (“Hey, my name is %s” % name)
Output:
Hey, my name is Meera
The %d operator allows you to add numbers within the strings. It automatically converts floating point numbers to decimal values.
Example:
value = 32
print(“Age of Meera is %d” % value)
Output
Age of Meera is 32