 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  ## Other Lessons for You

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)... 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... Debugging Python code
If you are getting an error while executing your python code, the better way of running your python code is by using -i switch. ex: python -i scriptname.py once you run the above command, your execution... Python - Solving Ordinary Differential Equations - Predator Prey Model aka Lotka Volterra Equations
This is an assignment in Python, I contributed to a numerical Python MOOC from George Washington University. The link to this assignment on github is here. The Lotka–Volterra equations, also...
S

DBMS - SQL - Any/All
All - Operator SELECT empno, sal FROM emp WHERE sal > ALL (1999, 2999, 3999); Output of Above query is same as below query SELECT empno, sal FROM emp WHERE sal > 1999 AND sal > 2999... ### Looking for Python Training classes?

Learn from Best Tutors on UrbanPro.

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you

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 