Python Programming Simplified: An Absolute Beginners Guide by Thada Vikas

Python Programming Simplified: An Absolute Beginners Guide by Thada Vikas

Author:Thada, Vikas [Thada, Vikas]
Language: eng
Format: epub
Published: 2021-05-06T00:00:00+00:00


6.10 The format method

The format method of str class has so much to offer that’s why we have devoted an entire section for this. As the name suggest the format method does the task of formatting the output. There are number of formatting options which you can use for various data types. The function takes variable number of arguments and keyword arguments that act as substitution for the placeholders represented using { }. Let’s take a simple example first:

name='chinmay'

print("Hello {}".format(name))

s="Hello {}"

print(s.format(name))

OUTPUT:

Hello chinmay

Hello chinmay

Here {} is the placeholder where the value of the variable name will be substituted. In the example two different ways have been shown to use format method. On with string literal and another with string variable. You can use any method which you prefer.

Lets see next example with multiple variables/literals.

print("Hello {} , you are {} years old".format("aksh",10))

print("{} plus {} is {} ".format(10,10,10+10))

a=2.3;b=3.45

print("{} x {} ={}".format(a,b,a*b))

OUTPUT:

Hello aksh , you are 10 years old

10 plus 10 is 20

2.3 x 3.45 =7.935

The two examples seen above used the concept of automatic numbering where the placeholders were simply {}. The number of placeholders must match with the number of variables/literals in the format function. Runtime error may ensue if the preceding line is not true.

It is also possible and useful way to provides numbers or keywords inside braces to bind them with the positional or keyword arguments in format function. Let’s start with this in next sub section.

6.10.1 Positional Arguments in format function

The variables/literals in the format function are numbered from left to right starting from 0,1,2….The position numbers can be placed inside the {} to clearly specify the binding of variables/literals with the placeholders.

name='chinmay'

age=21

salary=75565.56

print("Hello{0}your age is {1} and salary is {2}".format(name,age,salary))

Here {0} is the placeholder for name, {1} for age and {2} for salary. You cannot change this positional numbering i.e. instead of 0,1,2 inside {} you would like to use {1},{2} and {3} then python will generate error.

builtins.IndexError: tuple index out of range

Instead of using variables in format function you can also write literals directly:

print("Hello {0} your age is {1} and salary is {2}".format("chinmay",21,75565.56))

Further if you change the positions, nothing harms but the meaning of string changes like:

print("Hello {1} your age is {2} and salary is {0}".format("chinmay",21,75565.56))

6.10.2 Number Formatting

For formatting numbers, the format command provides number of options. All the options are listed in the table x.1:

Table 6.2: Number formatting options in format function

Sr.No.

Format symbol

Meaning



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.