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 is a popular programming language. It was created by Guidovan Rossum, and released in 1991.
Python is a popular programming language. It was created by Guidovan Rossum, and released in 1991. It is used for: web development (server-side), software development, mathematics, system scripting. What...

Decorators In Python
A decorator takes in a function, adds some functionality and returns it. Functions and methods are called callable as they can be called. Any object which implements the special method __call__() is...

Getting started with Python IDLE
Now that we have looked at some example programs, we would like to learn how to begin the actual process of writing a program in Python. There are a variety of ways to tell Python to execute the code we...

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

Use IPython shell for better understanding of python
Hi All, Even though there are many editors like IDLE, PyDev, PyCharm are available for python, it is very advantageous to use ipython as an interactive shell for Pyhon. How to install it? pip install...
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