Showing posts with label String. Show all posts
Showing posts with label String. Show all posts

Friday, January 20, 2012

Python Note 4( String Formatting)

Code
import math 

for x in range(1,20) :
 print ("{0:2d} {1:3d} {2:5d}".format(x,x*2,x*3))
 
tmplate="My {} is {}"
print(tmplate.format("country","Sri Lanka"))
print(tmplate.format("name","Kanishka"))

tmplate="Hello {name} , your role is {role}"
print(tmplate.format(name="Kanishka",role="Admin"))
print(tmplate.format(name="Sampath",role="User"))

print("The value of 'PI' is approximately {0:0.3f}".format(math.pi))
print("The value of 'e' is approximately {0:0.5f}".format(math.e))

#old formatting
print('The value of PI is approximately %5.3f' % math.pi)
Result

© kani.stack.notez 2012 | Blogger Template by Enny Law - Ngetik Dot Com - Nulis