UrbanPro
true

Learn Python Training from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Learn Python : Formatting with the .format () method

B
Biswanath Banerjee
15/06/2018 1 0

A good way to format objects into your strings for print statements is with the string.format() method. The syntax is :
'String here {} then also {}'.format('something1','something2')
Example 1:
print("This is a string {}'.format('FORMATTED'))
output:
This is a FORMATTED string.
Example 2:
print('The {} {} {}'.format('fox','brown','quick'))
output: The fox brown quick
Now if we supply the index number for these format words in a order inside the {}, then the output will be according to the index position of these formatted words. for example,
Example 3:
print('The {2} {1} {0}'.format('fox','brown','quick'))
output: The quick brown fox
word fox brown quick
index position 2 1 0

Example 4:
print('The {q} {b} {f}'.format('f = fox',' b = brown',' q = quick'))
Output: The quick brown fox

Float formatting follows "{value:width:precision f)"
Example 5:
result = 100/777
result
0.1287001287001287
print('The result is {r:1.3f}'.format(r = result)
output: The resuslt is 0.129

 

 

1 Dislike
Follow 2

Please Enter a comment

Submit

Other Lessons for You

Assignment operators
Operation Syntax Comment Assignment = Control flow right to left Add AND += Incremental Subtract AND -= Decremental Multiply AND *= Multiplicative increment Division...

How to know if an object is iterable?
An object in python is said to be an iterable if it obeys one of the following two rules:- 1. Object must consist of __getitem__ method 2. Object must consist of __iter__ method. String objects generally...

Different types of data in python3
Python3 features Numbers(int,float,complex), String, Lists, Tuples, Dictionary and Sets type of data. long interger type numbers from python2 is no longer required. Below are examples of all python3 data...

Python Demo
https://www.youtube.com/watch?v=8AEhpZ4lbOI

Bigdata hadoop training institute in pune
BigData What is BigData Characterstics of BigData Problems with BigData Handling BigData • Distributed Systems Introduction to Distributed Systems Problems with Existing Distributed...
X

Looking for Python Training Classes?

The best tutors for Python Training Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Python Training with the Best Tutors

The best Tutors for Python Training Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more