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
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.
Deep Learning with Python by François Chollet(12563)
Hello! Python by Anthony Briggs(9911)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9794)
The Mikado Method by Ola Ellnestam Daniel Brolund(9775)
Dependency Injection in .NET by Mark Seemann(9335)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8292)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7758)
Grails in Action by Glen Smith Peter Ledbrook(7693)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(7018)
Microservices with Go by Alexander Shuiskov(6786)
Practical Design Patterns for Java Developers by Miroslav Wengner(6698)
Test Automation Engineering Handbook by Manikandan Sambamurthy(6638)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6409)
Angular Projects - Third Edition by Aristeidis Bampakos(6047)
The Art of Crafting User Stories by The Art of Crafting User Stories(5579)
NetSuite for Consultants - Second Edition by Peter Ries(5510)
Demystifying Cryptography with OpenSSL 3.0 by Alexei Khlebnikov(5312)
Kotlin in Action by Dmitry Jemerov(5061)
