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

Python Introduction
Python is an object-oriented programming language created by Rossum in 1989. It is ideally designed for rapid prototyping of complex applications. It has interfaces to many OS system calls and libraries...
I

Imran

0 0
0

Python programming - Applications
If you’re thinking of learning Python? Or if you recently started learning it? You may be asking yourself: "What exactly can I use Python for?" There are so many applications for python, but there...

A program to calculate Correlation Coefficient
Task: Calculating the Correlation-coefficient using Python We know that the correlation coefficient is calculated using the formula nΣxy- ΣxΣy / (√(nΣx^2-(Σx)^2)...

How Machine Learning Predict things - A Short Video for absolute beginners
Here is a short video for Machine Learning Beginners, who want to know how Machine Learning Algorithm predict things

What is Python Egg?
(Attention: This is a technical post for Python developers. If you are not python programmer you might want to skip this). An .egg format files in Python is the standard distribution package contains...
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