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

The Zen of Python
Writing programs that actually do what they are supposed to do is just one component of being a good python programmer. It's also important to write clean code that is easily understood, even weeks after...

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...

Python- Basic Concepts part 1
1. What is Python? Python is a high-level programming language, with applications in numerous areas, including web programming, scripting, scientific computing, and artificial intelligence. It is...

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...

Write your first Python program in 10 minutes
1. Download python from python official site search "python download" in google 2. Install in your machine 3. verify using : "python --version" command 4. Write first program using notepad create...
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